Warren Repole

Don't Be a SAS® Dinosaur: Modernize Your SAS Programs
by Warren Repole

Diagnostics: Write Messages to the SAS Log


Scenario:

While writing text to an external file in a DATA step, you want to generate diagnostic messages to the SAS log when suspicious data is encountered.

The destination of text written by the PUT statement is determined by the previously executed FILE statement.


The old way: Switch Between the External File and the SAS Log

The original approach is to execute a FILE LOG statement prior to each diagnostic message, and then re-execute the FILE statement for the output file after the diagnostic message is complete.

Download this program
data _null_;
  file "c:\temp\Dinosaur\PutLogFile1.csv" dsd;
  if _n_=1 then put "Name,Age,Height";
  set sashelp.class;
  if age lt 12 then do;
    file log;
    put "Student age is less than 12 ... Please verify for " name= age=;
    file "c:\temp\Dinosaur\PutLogFile1.csv";
  end;
  put Name Age Height;
run;

SAS Log

1078  data _null_;
1079    file "c:\temp\Dinosaur\PutLogFile1.csv" dsd;
1080    if _n_=1 then put "Name,Age,Height";
1081    set sashelp.class;
1082    if age lt 12 then do;
1083      file log;
1084      put "Student age is less than 12 ... Please verify for " name= age=;
1085      file "c:\temp\Dinosaur\PutLogFile1.csv";
1086    end;
1087    put Name Age Height;
1088  run;
 
NOTE: The file "c:\temp\Dinosaur\PutLogFile1.csv" is:
      Filename=c:\temp\Dinosaur\PutLogFile1.csv,
      RECFM=V,LRECL=256,File Size (bytes)=0,
      Last Modified=14May2008:20:21:17,
      Create Time=05Aug2007:18:06:13
 
Student age is less than 12 ... Please verify for Name=Joyce Age=11
Student age is less than 12 ... Please verify for Name=Thomas Age=11
NOTE: 20 records were written to the file "c:\temp\Dinosaur\PutLogFile1.csv".
      The minimum record length was 10.
      The maximum record length was 15.
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
 
raptor

The new way: Write to both the External File and the SAS Log
(available in SAS 9)

An alternate approach is to use the PUTLOG statement for the diagostic messages.

Download this program
data _null_;
  file "c:\temp\Dinosaur\PutLogFile2.csv" dsd;
  if _n_=1 then put "Name,Age,Height";
  set sashelp.class;
  if age lt 12 then
    putlog "Student age is less than 12 ... Please verify for " name= age=;
  put Name Age Height;
run;

SAS Log

1100  data _null_;
1101    file "c:\temp\Dinosaur\PutLogFile2.csv" dsd;
1102    if _n_=1 then put "Name,Age,Height";
1103    set sashelp.class;
1104    if age lt 12 then
1105      putlog "Student age is less than 12 ... Please verify for " name= age=;
1106    put Name Age Height;
1107  run;
 
NOTE: The file "c:\temp\Dinosaur\PutLogFile2.csv" is:
      Filename=c:\temp\Dinosaur\PutLogFile2.csv,
      RECFM=V,LRECL=256,File Size (bytes)=0,
      Last Modified=14May2008:20:21:17,
      Create Time=05Aug2007:18:07:45
 
Student age is less than 12 ... Please verify for Name=Joyce Age=11
Student age is less than 12 ... Please verify for Name=Thomas Age=11
NOTE: 20 records were written to the file "c:\temp\Dinosaur\PutLogFile2.csv".
      The minimum record length was 10.
      The maximum record length was 15.
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
 

Advantages of the alternate approach: Good

  • The PUTLOG statement writes only to the SAS log, eliminating any potential interference with the external output file.
  • The only FILE statement required corresponds to the external output file. The original approach may require the main FILE statement to be executed multiple times.

Disadvantages of the alternate approach: Bad

  • The PUTLOG statement writes only to the SAS log, so you cannot redirect diagnostic messages to a secondary external file. You must specify multiple FILE statements, one for the main external file and one for the diagnostics.

Additional documentation for this technique can be found in SAS® 9.2 Language Reference: Dictionary. Cary, NC: SAS Institute Inc.

Visit http://support.sas.com/documentation/onlinedoc/sas9doc.html for SAS 9 documentation.

The URL for this page is http://www.repole.com/dinosaur/putlog.html

These techniques are mentioned in other SAS references and publications:


Back to SAS Dinosaur home page

Printable copy of this page (without sample output)

SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries.

® indicates USA registration.