数据如下:
ID Var1 Var2
A 12
A 3 4
B 5 6
B 7 8
C 9 10
D. 11 12
我试图在SAS中从上面的主表创建子集表,这样每个子集表都有来自主表的所有记录,其中ID等于每个不同的值。因此,在这个特殊的例子中,我想动态地创建4个不同的表-- Tab_A、Tab_B、Tab_C、Tab_D,其中Tab_A包含来自主表ID=A等的所有记录。表总数为ID的created=distinct值。
发布于 2015-11-17 18:24:56
这是创建宏的好案例。SAS宏动态编写SAS代码。
这个宏读取不同的ID值,然后生成数据步骤来执行子集。它使用mprint
选项,这样您就可以查看它创建的代码。
data have;
input ID $ Var1 Var2;
datalines;
A 1 2
A 3 4
B 5 6
B 7 8
C 9 10
D 11 12
;
run;
%macro subset();
proc sql noprint;
select count(distinct id)
into :nIDs TRIMMED
from have;
select distinct ID
into :ID1 - :ID&nIDs
from have;
quit;
data
%do i=1 %to &nIDs;
tab_&&id&i
%end;
;
set have;
select (ID);
%do i=1 %to &nIDs;
when ("&&id&i") output tab_&&id&i;
%end;
otherwise put "PROBLEM";
end;
run;
%mend;
options mprint;
%subset;
下面是显示实际执行子集的SAS数据步骤的日志。
MPRINT(SUBSET): data tab_A tab_B tab_C tab_D ;
MPRINT(SUBSET): set have;
MPRINT(SUBSET): select (ID);
MPRINT(SUBSET): when ("A") output tab_A;
MPRINT(SUBSET): when ("B") output tab_B;
MPRINT(SUBSET): when ("C") output tab_C;
MPRINT(SUBSET): when ("D") output tab_D;
MPRINT(SUBSET): otherwise put "PROBLEM";
MPRINT(SUBSET): end;
MPRINT(SUBSET): run;
https://stackoverflow.com/questions/33763566
复制相似问题