1

This is a rather stupid example but it keeps the essense of what I am trying to do (using SAS university edition):

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;

The problem with this is when I run this code I got this error message:

WARNING: Apparent symbolic reference COL not resolved.
&col

Why is this. Does not sas change &col with its true value at run time?

UPDATE: Setting "&t" instead of '&t' solved my problem. The code is now working.

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;
Joe
  • 62,789
  • 6
  • 49
  • 67
fossekall
  • 521
  • 1
  • 10
  • 27

1 Answers1

1

Several issues here

where tables='&t' will not work because of the single quotes. You have to use double quotes whenever using macro variables.

Also &t is not defined

This seems to work (i.e. printing cul in the log) , but I had to define t manually.

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;
stallingOne
  • 3,633
  • 3
  • 41
  • 63