id | Var | low | up | cls |
1 | age | 3 | 5 | c1 |
2 | age | 5 | 8 | c2 |
3 | age | 8 | 11 | c3 |
[/table]我们希望给到任意一个数据集t2,比如:
[table=50%]
idage
123
23
34
48
510 可以用t1的上下限给t2的数据集标记类(cls).
简单一点,宏的循环我直接指定了,看官如果需要可以自己改。
第一步:生成if语句 (autop)
%macro autop;
data ot;
length sen $ 1000 ;
sen='';
stop;
run;
%do i=1 %to 3;
data tem;
set t1;
if _n_ = &i ;
call symput('var',var);
call symput('low',low);
call symput('up',up);
call symput('cls',cls);
run;
data tem1;
length sen $ 1000;
/*sen='if '||compress(symget('low'))||' < '||compress(symget('var'))||' <= '||compress(symget('up'))||' then '||"%sysfunc(cats(&var,_c))"||'='||"'"||compress(symget('cls'))||"'"||';';*/
sen=catx(' ','if',compress(symget('low')),'lt',compress(symget('var')),'le',compress(symget('up')),'then',
"%sysfunc(cats(&var,_c))",'=',"'",compress(symget('cls')),"'",';');
run;
proc append base=ot data=tem1 force;
run;
%end;
%mend;
%autop;
结果:
第二步:生成一个外部sas文件 (ppp)
%let set=t2;
filename test 'e:\test.sas';
%macro ppp;
data _null;
set ot end=last;
file test;
if _n_=1 then put "data new; set t2;";
put sen;
if last then put 'run;';
run;
%mend;
%ppp
第三步:引用生成的文件
%include "e:\test.sas";
结果: