Monday, July 10, 2017

Hack 5.5 Using Macros to Comment Out Sections of SAS Programs



SAS Programming Professionals,

Did you know that you can use SAS macros as a handy way to comment out code in a program?

You can do this by enclosing portions in a SAS macro and not calling that macro.  Here is an example:

options msglevel=I;

%MACRO HIDEIT;

libname audit "H:\My Documents\My SAS Programs\Audit Trails\Data";

proc copy in=sashelp out=audit;
      select orsales;
run;

data audit.orsales;

seqno = _n_;

set  audit.orsales;

run;

%MEND HIDEIT;

data audit.orsales2;
set  audit.orsales;
stop;
run;

proc datasets library=audit nolist;
      audit orsales2;
      initiate;
      log admin_image=yes
      before_image=yes
      data_image=no
      error_image=yes;
run;
quit;

In this example, we wish to not execute (to hide) the LIBNAME statement, PROC COPY, and the DATA step as we test the latter sections of our program.  So, after they are first executed, we enclose them in the HIDEIT macro.  Thereafter, when we run this program, the aforementioned SAS constructs do not execute.  Once we are done testing the program, we can either comment out the %MACRO and %MEND statements or delete them entirely.  Pretty clever, eh?

Best of luck in all your SAS endeavors!


---MMMMIIIIKKKKEEEE
(aka Michael A. Raithel)
Author of the new cult classic for computer programmers:  It Only Hurts When I Hit <ENTER>
Print edition:  http://tinyurl.com/z8bzx2e 
Kindle edition: http://tinyurl.com/zypgqa7 

The hack above is an excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers

No comments:

Post a Comment