huanfeng125 发表于 2011-3-25 10:39 
1、现在我有一个数据集,里面有一个变量a,数据内容如:1 1 3 4 1 3 5 4 6 5 5 2 4 3 2 6 5 4 6 2 3……,这个变量最小数是1,最大数是80.每个数字都有很多重复的数,如有10个1,20个2,25个3,34个4……。
问题:将有相等a值的观测输出到同一个数据集,数据集名字以字母a开头,以a的值结尾,如a1,a2,a3,a4,……依次类推。可否用循环将不同的观测输出到不同数据集?我自己用一个宏,然后要调用80个宏,有点麻烦,所以想设置一个循环。
Here is a simple way of using call execute in a dynamic way.
%let f=8;
data t1;
do i=1 to 10;
a=ceil(ranuni(123)*&f);
output;
end;
keep a;
run;
proc freq data=t1 noprint;
table a /out=t2;
run;
data _null_;
length dsn $10 ifstmt $30;
call execute('data ');
do i=1 to nobs;
set t2 point=i nobs=nobs;
dsn=compress("A_"||put(a,6.));
call execute(dsn);
end;
call execute('; set t1;');
do i=1 to nobs;
set t2 point=i nobs=nobs;
dsn=compress("A_"||put(a,6.));
ifstmt=catx(" ", "if a=", put(a,6.)," then output ", dsn, ";");
call execute(ifstmt);
end;
call execute('run;');
stop;
run;