我有一个包含多个"XO代码"的id列表。我想创建一个宏,它将循环遍历这些id,并使用与相应的XO代码对应的where语句为每个id创建一个表。例:
%let ID_77= '35X02','35X04';
%let DnO_IDs= &ID_77; /intends to add more &ID_ numbers/
%macro loop;
proc sql;
%do k=1 %to %sysfunc(countw(&DnO_IDs_Ids.));
%let ID= %scan(&DnO_IDs.,&k.);
create table EP_&ID as
select * from table
where XO in ("&ID.") and AY>=(&CurrY-14);
%end;
quit;
%mend;
%loop;
我收到这个错误:错误:在需要数字操作数的%EVAL函数或%IF条件中发现了字符操作数。条件是:"35 x04"错误:宏函数%SCAN的参数2不是一个数字。错误:宏LOOP将停止执行。
引号是一个问题,不应该需要。使用逗号作为分隔符也会带来麻烦。使用空格会更好。
%let ID_77= 35X02 35X04;
%let DnO_IDs= &ID_77; /intends to add more &ID_ numbers/
%let CurrY=2015;
%macro loop;
%local k id ;
proc sql;
%do k=1 %to %sysfunc(countw(&DnO_IDs));
%let ID= %scan(&DnO_IDs,&k);
create table EP_&ID as
select * from table
where XO in ("&ID") and AY>=(&CurrY-14)
;
%end;
quit;
%mend;
%loop;
如果您确实想使用引号分隔列表的值,那么您需要适当地修改COUNTW()
和%SCAN()
函数调用,并添加对DEQUOTE()
的调用以删除引号。
%let ID_77= '35X02','35X04';
... %sysfunc(countw(%superq(DnO_IDs),%str(,)));
%let ID= %sysfunc(dequote(%scan(%superq(DnO_IDs),&k,%str(,))));
我认为这应该可以解决问题:%做k = 1% % eval (% sysfunc (countw (DnO_IDs_Ids。))),
可能是%sysfunc返回值被认为是非整数。