Monday, February 22, 2016

Hack 2.3 Creating Custom Log Messages with the PUTLOG Statement


SAS Programming Professionals, 

Did you know that you can create your own custom messages and have them written to the SAS log? 

Custom messages can be written to the log in a DATA step via the PUTLOG statement.  The PUTLOG statement comes in handy when you want to flag specific issues with the values of variables within an observation.  For example, consider this program: 

data vetted_class;
set  sashelp.class; 

if age < 12 then do; 

      putlog 'MY_WARNING: This subject too young for study: '
              name 'age= ' age '.';
      putlog 'MY_WARNING: Subject deleted from vetted data set.';
      putlog ' ';
      delete;
end; 

run;

In this example, we want the VETTED_CLASS data set to contain only subjects who are 12 years of age or older.  The IF statement is used to delete observations that do not meet that criteria.  However, without a warning message in the log, there is no audit trail of the subjects that were deleted and the age that disqualified them. 

The first PUTLOG statement prints a warning message along with the name and age.  The second PUTLOG statement simply prints a warning message.  The third PUTLOG message prints a blank line for better readability in cases where there are many rejected subjects. 

The SAS log for this program looks like this: 

1    data vetted_class;
2    set  sashelp.class;
3
4    if age < 12 then do;
5
6        putlog 'MY_WARNING: This subject too young for study: ' name 'age= ' age '.';
7        putlog 'MY_WARNING: Subject deleted from vetted data set.';
8        putlog ' ';
9        delete;
10
11   end;
12
13   run; 

MY_WARNING: This subject too young for study: Joyce age= 11 .
MY_WARNING: Subject deleted from vetted data set.

MY_WARNING: This subject too young for study: Thomas age= 11 .
MY_WARNING: Subject deleted from vetted data set.

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.VETTED_CLASS has 17 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds

Judicious use of the PUTLOG statement can add that extra layer of QC that helps to make you one of your organization's top SAS programmers!
Best of luck in all of your SAS endeavors! 

----MMMMIIIIKKKKEEEE

(aka Michael A. Raithel)
Excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers

I plan to post each and every one of the hacks in the book to social media on a weekly basis.  Please pass them along to colleagues who you know would benefit.