Write SAS log to text file and log window

In the blog post Output Log to Text file in SAS with Proc Printto, I demonstrate how to route the SAS log to an external text file. However, when we do this, no log is displayed in the usual log window. Browsing the various online code communities, you will see quite a few requests for the ability to export the log to an external text file and display the log in the usual window in SAS. As of today, this feature is not directly available in SAS. Today I will demonstrate a workaround so that you can display the log, as usual, and save an external copy.

A Workaround

The workaround is simple. First, export the log to an external text file with Proc Printto. Exactly like in the post at the top. Next, run the code you want logged. Now the log is in the text file. But not in the log window. Therefore, I read the log in the text file line by line and use the Putlog Statement to display each line in the log. I utilize the fact that the _infile_ variable contains the input buffer content. Therefore, I simply display the contents of _infile_ in the log. Now the SAS log exists both as a text file and in the log window.

/* Specify the path for the external log */
%let path=YourPathHere;
 
/* Export the log to a text file         */
proc printto log="&path.";
run;
 
/* Run some SAS code                     */
data class;
   set sashelp.class;
run;
 
proc sort data=class;
run;
 
proc print data=class;
run;
 
/* Re-reoute the log to the log window  */
proc printto log=log; run;
 
/* Read the log in the text file and 
   display each line in the log          */
data _null_;
   infile "&path.";
   input;
   putlog "*>" _infile_;
run;

Summary

In this post, I demonstrate a simple trick of how you can write the SAS log to both a text file and in the log window. SAS can not write the log to more than one location at once. Therefore, I first route it to an external file with Proc Printto. Afterwards, I read the entire log into SAS again and print each line in the log window.

As a related post, see Clear Log and Results Window in SAS.

I can’t take the credit for this trick. I learned this trick from the user Yabwan at the SAS Community.

You can download the entire code from this post here.