在 SAS 中创建观察组合
Creating combinations of observations in SAS
我需要弄清楚如何将数据集中所有可能的数据组合制成表格。我有一个数据集,其中每个人有 2 行,一行表示 activity 分数,一行表示测试总分。每次访问的分数都有变量。一个人可能有 1 到 5 次访问。我正在为每个分数寻找给定人的所有可能分数组合。
例如,这里是生成示例数据结构的代码。
data example;
input name $ type $ visit1-visit5;
datalines;
Bob activity 10 13 16 . .
Bob total 13 19 17 . .
John activity 11 20 25 20 21
John total 13 15 17 19 22
Steve activity 6 . . . .
Steve total 9 . . . . .
;
run;
我想要一个数据集,它可以给我一个结构如下:
Bob activity 10 13
Bob activity 10 16
Bob activity 13 16
Bob total 13 19
Bob total 13 17
Bob total 19 17
John (rows for all possible combinations)
Steve - would have no rows, since he only has one visit (no combinations possible)
有什么建议吗?
对于 N 选择 2,你想要的输出结构有几个嵌套的 DO 就足够了。
data example;
input name $ type $ visit1-visit5;
datalines;
Bob activity 10 13 16 . .
Bob total 13 19 17 . .
John activity 11 20 25 20 21
John total 13 15 17 19 22
Steve activity 6 . . . .
Steve total 9 . . . . .
;;;;
run;
data by2;
set example;
array v[*] visit:;
n=n(of v[*]);
do i = 1 to n;
col1 = v[i];
do j = i + 1 to n;
col2 = v[j];
output;
end;
end;
drop i j visit:;
run;
proc print;
run;
我需要弄清楚如何将数据集中所有可能的数据组合制成表格。我有一个数据集,其中每个人有 2 行,一行表示 activity 分数,一行表示测试总分。每次访问的分数都有变量。一个人可能有 1 到 5 次访问。我正在为每个分数寻找给定人的所有可能分数组合。
例如,这里是生成示例数据结构的代码。
data example;
input name $ type $ visit1-visit5;
datalines;
Bob activity 10 13 16 . .
Bob total 13 19 17 . .
John activity 11 20 25 20 21
John total 13 15 17 19 22
Steve activity 6 . . . .
Steve total 9 . . . . .
;
run;
我想要一个数据集,它可以给我一个结构如下:
Bob activity 10 13
Bob activity 10 16
Bob activity 13 16
Bob total 13 19
Bob total 13 17
Bob total 19 17
John (rows for all possible combinations)
Steve - would have no rows, since he only has one visit (no combinations possible)
有什么建议吗?
对于 N 选择 2,你想要的输出结构有几个嵌套的 DO 就足够了。
data example;
input name $ type $ visit1-visit5;
datalines;
Bob activity 10 13 16 . .
Bob total 13 19 17 . .
John activity 11 20 25 20 21
John total 13 15 17 19 22
Steve activity 6 . . . .
Steve total 9 . . . . .
;;;;
run;
data by2;
set example;
array v[*] visit:;
n=n(of v[*]);
do i = 1 to n;
col1 = v[i];
do j = i + 1 to n;
col2 = v[j];
output;
end;
end;
drop i j visit:;
run;
proc print;
run;