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;

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

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