BookmarkSubscribeRSS Feed
JesperMusaeus
SAS Employee

 

The Macro language is powerfull, but sometimes it would be nice if you could avoid making a macro, just to make a small part of your code conditional. For example: when your program should delete a tabel - but only if it exist (to avoid warnings in the log).

 

%put Warnings if you drop a a table that doesnt exist;
proc sql ;
   drop table WORK.CLASS;
quit;
 
options mprint;
%put Method 1 (Old school): wrap the code in a macro deleting the table if it exists - call macro;
 
%macro mymacro;
%if %sysfunc(exist(WORK.CLASS)) %then
   %do;
      proc sql ;
         drop table WORK.CLASS;
      quit;
   %end;
%mend;
 
%mymacro;
 
***Same but simpler (no macro definition) using SAS function IFC.;
%put Method 2: use IFC - avoid new macro;
 
proc sql ;
   %sysfunc(IFC(%sysfunc(exist(WORK.CLASS)), drop table WORK.CLASS, )); ;
quit;

 

Also with SAS 9.4 Maintenance 5 You can now use %IF-%THEN-%ELSE constructs in open code

 

https://blogs.sas.com/content/sasdummy/2018/07/05/if-then-else-sas-programs/

 

%put Method 3: SAS 9.4M5 only - use in open code;

%if %sysfunc(exist(WORK.CLASS)) %then
   %do;
      proc sql ;
         drop table WORK.CLASS;
      quit;
   %end;