SAS宏变量确实无法解决

时间:2017-02-14 11:49:08

标签: sas sas-macro

这是一个相当愚蠢的例子,但它保留了我想要做的事情(使用SAS大学版):

data TableList;
input tables $ cols  $;
cards;
tab1 col
tab2 cul
;
run; 


%macro test; 
    proc sql;
      select tables
        into:tabs separated by " "          
      from TableList;
    quit;   

    %do i=1  %to 2;           
       %let t = %scan(&tabs,&i);       
       proc sql;
          select cols
        into: col           
          from TableList
          where tables='&t';
       quit;   
       %put &col;          
    %end;   
%mend;
%test;

问题是当我运行此代码时收到此错误消息:

WARNING: Apparent symbolic reference COL not resolved.
&col

为什么会这样。在运行时不会改变它的真实值吗?

更新: 设置"& t"而不是'& t'解决了我的问题。代码现在正在运行。

data TableList;
input tables $ cols  $;
cards;
tab1 col
tab2 cul
;
run; 

%macro test; 
    proc sql;
      select tables
        into:tabs separated by " "          
      from TableList;
    quit;   
    %do i=1  %to 2;           
       %let t = %scan(&tabs,&i);       
       proc sql;
          select cols
        into: col           
          from TableList
          where tables="&t";
       quit;   
       %put Column &col;           
    %end;   
%mend;
%test;

1 个答案:

答案 0 :(得分:1)

这里有几个问题

由于单引号,

where tables='&t'无效。每当使用宏变量时都必须使用双引号。

&t未定义

这似乎有用(即。打印cul在日志中),但我必须手动定义t

data TableList;
    input tables $ cols  $;
    cards;
tab1 col
tab2 cul
;
run;

%let t=tab2;

%macro test;

    proc sql;
        select tables
            into:tabs separated by " "          
        from TableList;
    quit;

    %do i=1  %to 2;
        %let t = %scan(&tabs,&i);

        proc sql;
            select cols
                into: col           
            from TableList
                where tables="&t";
        quit;

        %put &col;
    %end;
%mend;

%test;