我有一个组宏变量,我想将其类放入宏变量中。例如:
%macro test(group=);
proc freq data=foll;
tables &group / out=freqtbl;
run;
proc sql;
create table grp
(grpid char(4));
insert into grp
values('a')
values('b')
;
quit;
data freqtbl1;
merge grp freqtbl;
run;
data freqtbl2;
set freqtbl1;
call symput(grpid,&group);
run; * &a is the first group, &b is the second group;
%mend;
Run Code Online (Sandbox Code Playgroud)
这适用于 2 个类,但如果有 3 个或 3 个以上类怎么办?
多谢。
proc sql;
select distinct age into :cls1-:cls10
from sashelp.class;
quit;
%put &cls1 &cls2 &cls3 &cls4 &cls5 &cls6 &cls7;
Run Code Online (Sandbox Code Playgroud)
使用此功能时,您仍然会遇到知道何时停止的问题(如上面所示,&cls7未解决)。将 或其他值设置-:cls10为足够高的值,以免耗尽变量。
如果您实际上希望将其放在一个变量中,
select distinct age into :cls separated by ' '
Run Code Online (Sandbox Code Playgroud)
将使 &cls 包含其中的所有值。
您也可以在数据步骤中执行此操作 - 类似于:
data _null_;
set mydata;
by myvar;
initialvar=65;
if first.myvar then do;
call symput(byte(initialvar),myvar);
initialvar+1;
end;
run;
Run Code Online (Sandbox Code Playgroud)
这需要您对其进行排序。您也可以从PROC FREQ结果数据集运行一组类似的代码。