-2

Try again. This code will not work. It is a stupid code but still do not work.

data work.colnames;
      input cols $;
      cards;
      U1
      B1
      ;
run;


data work.test;
      input rp_U1 $ rp_B1 $;
      cards;
      col1 col2
      ;
run;

%macro maketest;
      proc sql;
           select cols
                  into :col separated by " "
            from colnames;         
      quit;
      %do i=1 %to 2;                          
            %let c = %qscan(&col,&i);                           
            %put rp_&c;
            proc sql;
                  create table test&i  as
                  select                 
                  rp_&c                  
                from work.test;             
            quit;    
    %end;
%mend;
%maketest;

I do get this error:

 ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, *,   **, +, ',', -, '.', /, <, <=, <>, =, >, >=, ?, AND, AS, 
           CONTAINS, EQ, EQT, GE, GET, GT, GTT, LE, LET, LIKE, LT, LTT, NE,   NET, OR, ^=, |, ||, ~=.  

Which I find strange is if I try to get the columns without the rp_ this code works find. Also change

select                 
rp_&c   

to

select                 
&c  
Joe
  • 62,789
  • 6
  • 49
  • 67
fossekall
  • 521
  • 1
  • 10
  • 27
  • turn on mprint (`options mprint;`) and attach the log to the question. It will be much easier for us to help you debug the issue. – DomPazz Feb 14 '17 at 21:16

1 Answers1

2

The macro quoting you introduced by using %QSCAN() function instead of the %SCAN() function is probably causing trouble for the SAS parser.

In your code the macro variable C gets assigned the values of U1 and B1, respectively. But the values are macro quoted. So when the parser sees rp_&c it thinks that those are two separate tokens so it treats it like token rp_ followed by the token U1 instead of a single token of rp_U1.

You should not need to quote a string that will be part of a variable name, so you should change

%let c = %qscan(&col,&i);

to

%let c = %scan(&col,&i);

But if you do need to have the macro quoting then you can use the %unquote() function to remove it. So change

select rp_&c

to

select %unquote(rp_&c)
Tom
  • 47,574
  • 2
  • 16
  • 29