Search code examples
sassas-macro

macro variable not being resolved correctly in SAS


I have a SAS macro like this:

%macro test;
proc sql;
SELECT count(*) INTO:Nbr_Obs from table1;
quit;
%put  observations=&Nbr_Obs;
%if &Nbr_Obs=0 %then %do;
proc sql;
  drop table table1 ;
quit;
%end;
%mend;

Neither the %put statement is showing up in the log, and the 'if' condition is always evaluating to true. How can I resolve this?

EDIT: This is what I see in log:

MPRINT(TEST):   proc sql;
MPRINT(TEST):   SELECT count(*) INTO:Nbr_Obs from TABLE1;
MPRINT(TEST):   quit;
observations=
...
...

And after this, it shows that SQL is executing. So it seems that the PU T statement is executing before the SQL?


Solution

  • It is always a best practise to strip leading and trailing blanks from macro variables created using INTO clause, this is the reason for incorrect comparison. Please modify as below,

    %macro test;  
    
       proc sql;
         SELECT count(*) INTO:Nbr_Obs 
         from table1;
       quit;  
    
       %let Nbr_Obs=&Nbr_Obs; * Strips blanks;  
       %put  observations=&Nbr_Obs;  
    
       %if &Nbr_Obs=0 %then %do;  
         proc sql;  
           drop table table1 ;  
         quit;  
    
       %end;  
    
    %mend;