我部分地有一个使用联合查询的解决方案,proc sql
其中包含子查询来实现匿名人员的姓名和号码。
但是正如您所注意到的,每个人都是手动输入到每个选择查询中的。您不愿意为数据集中的 200 人而不是示例中显示的 5 人执行此操作。一种可能性是运行插入查询以反映联合的选择查询:
proc sql;
create table AnonymousReport As
SELECT CASE WHEN t1.name = 'Alice' THEN t1.name ELSE CATS('Person',
(SELECT count(name) + 1 From have t2
WHERE t2.name <= t1.name AND t1.name ne t2.name)) END As RptName,
t1.Col1Number, t1.Col1Pct, t1.Col2Number, t1.Col2Pct, 'Alice' As ReportToWhom
FROM have t1
UNION ALL
SELECT CASE WHEN t1.name = 'Bob' THEN t1.name ELSE CATS('Person',
(SELECT count(name) + 1 From have t2
WHERE t2.name < t1.name AND t1.name ne t2.name)) END As RptName,
t1.Col1Number, t1.Col1Pct, t1.Col2Number, t1.Col2Pct, 'Bob' As ReportToWhom
FROM have t1
UNION ALL
SELECT CASE WHEN t1.name = 'Carol' THEN t1.name ELSE CATS('Person',
(SELECT count(name) + 1 From have t2
WHERE t2.name < t1.name AND t1.name ne t2.name)) END As RptName,
t1.Col1Number, t1.Col1Pct, t1.Col2Number, t1.Col2Pct, 'Carol' As ReportToWhom
FROM have t1
UNION ALL
SELECT CASE WHEN t1.name = 'Dave' THEN t1.name ELSE CATS('Person',
(SELECT count(name) + 1 From have t2
WHERE t2.name < t1.name AND t1.name ne t2.name)) END As RptName,
t1.Col1Number, t1.Col1Pct, t1.Col2Number, t1.Col2Pct, 'Dave' As ReportToWhom
FROM have t1
UNION ALL
SELECT CASE WHEN t1.name = 'Erin' THEN t1.name ELSE CATS('Person',
(SELECT count(name) + 1 From have t2
WHERE t2.name < t1.name AND t1.name ne t2.name)) END As RptName,
t1.Col1Number, t1.Col1Pct, t1.Col2Number, t1.Col2Pct, 'Erin' As ReportToWhom
FROM have t1;
quit;
输出数据集。从这里按最后一列 ReportToWhom 导出每个人的个人报告
RptName Col1Number Col1Pct Col2Number Col2Pct ReportToWhom
Alice 4 15% 8 20% Alice
Person2 8 30% 6 15% Alice
Person3 4 15% 8 20% Alice
Person4 4 15% 8 20% Alice
Person5 4 15% 8 20% Alice
Person1 4 15% 8 20% Bob
Bob 8 30% 6 15% Bob
Person3 4 15% 8 20% Bob
Person4 4 15% 8 20% Bob
Person5 4 15% 8 20% Bob
...
一种可能的解决方案是在数据集的所有行中使用连接的插入 SQL 查询:
data concat;
set have;
length reptAll $3200;
by name;
retain reptAll;
if first.name then reptAll = "";
unionSQL = "INSERT INTO AnonymousReport (RptName, Col1Number, Col1Pct, Col2Number, Col2Pct, ReportToWhom)
SELECT CASE WHEN t1.name = '" || name || "' THEN t1.name
ELSE CATS('Person', (SELECT count(name) + 1 From have t2
WHERE t2.name <= t1.name AND t1.name ne t2.name)) END As RptName,
t1.Col1Number, t1.Col1Pct, t1.Col2Number, t1.Col2Pct, '" || name || "' As ReportToWhom
FROM have t1";
reptAll = catx('; ', reptAll, unionSQL) ;
call symput('query', reptAll);
if last.name then output;
run;
然后将字符串传递给proc sql
宏:
%macro runsql;
proc sql;
&query;
quit;
%mend runsql;
%runsql;
在 R 中,我可以使用它的paste
/ for
loop/apply
函数在几秒钟内完成此操作,但 SAS 语法是另一个世界!