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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

Discussion stats
  • 0 replies
  • 956 views
  • 6 likes
  • 1 in conversation