首页 > 解决方案 > 如何将 &&var&i 放入 if 条件

问题描述

/*create macro variables*/
    PROC SQL NOPRINT;
    SELECT RESTRICTIONS
    INTO :RESTRI1 - :RESTRI35 
    FROM SASDATA.RESTRICTIONLIST; 
    QUIT;
    %PUT &RESTRI2;

/*the resolved value is: */
gender = 'M' and state = 'CA'

sasdata.newlist&i当第 i 个限制为&&restri&i(例如:gender = 'M'state = 'CA')时,我想创建一个数据集。&&restri&I*我只想要在这个新创建的数据集中满足限制的观察

虽然 sasdata.newlist2 包含 sasdata.oldlist 中的所有数据,但 if 条件不起作用。有人可以帮我解决这个问题吗?

%Macro testing(I);
 data sasdata.newlist&i;
 set sasdata.oldlist;
 %if &&restri&i %then; 
 run;
%mend testing;
%testing(2)

标签: if-statementmacrossas

解决方案


您没有在适当的上下文中解析宏变量。应用限制代码时,解决它,以便可以将其编译(数据逐步)作为 DATA 步的一部分。

%Macro testing(I);
 data sasdata.newlist&i;
 set sasdata.oldlist;

 /* %if &&restri&i %then;  NO-no-no, incorrect context */

 * apply ith restriction as a sub-setting IF statement;
 if &&restri&i;

 run;
%mend testing;
%testing(2)

推荐阅读