After successful execution of each SAS statement SAS generates a log and you can see it in the LOG window assuming you’re running your code in SAS Studio or SAS EG.
It’s always a good practice to check out the logs even after the successful execution of the SAS program. It gives you a sort of surety that everything executed well without any error, warnings or unwanted notes.
Maintaining log files is also a recommended practice that you should exercise. Fpr that you need to generate and save SAS log into an external file. Writing SAS log into an external file is very easy in SAS with the PROC PRINTTO procedure.
There are multiple ways to save SAS log file into an external location but the proc printto procedure is more handy to use.
How to Save SAS Log File into an external location using PROC PRINTTO procedure
PROC PRINTTO:
The PRINTTO procedure defines destinations for SAS procedure output and for the SAS log. You can store the SAS log or procedure output in an external file or in a SAS catalog entry.
With additional programming you can use SAS log files as input data for other purposes such as creating a dashboard which shows execution time for each program you executed in a day. There could be many other use cases as well.
Syntax:
PROC PRINTTO (options) ;
PROC PRINTTO Options:
- LABEL= : It provides a description for a sas log or procedure output stores in a SAS catalog entry.
- LOG= external-file-name-with-path or SAS-catalog-entry : with this option you tell SAS to route all the SAS logs to a permanent external file or SAS catalog entry.
- NEW : This option helps you to clean content from an existing file so SAS can write fresh logs for the current execution. It replaces the existing file instead of appending data.
- PRINT= : It routes procedure output to a permanent external file or SAS catalog entry or printer.
- UNIT=nn : It routes the output to the file identified by FILEREF.
PROC PRINTTO Program structure
proc printto log='log-file' new;
run;
You can use the FILENAME statement to assign reference to your external file and use it in the PROC PRINTTO procedure.
filename fileref 'log-file' ;
proc printto log=fileref new;
run;
You must add a blank proc printto procedure at the end so it can restore redirection. It means after routing the log to an external file, you must specify LOG to route the SAS log back to its default destination.
proc printto;
run;
PROC PRINTTO Block Structure
filename fileref 'log-file' ;
proc printto log=fileref new;
run;
/* ...write your code here */
proc printto;
run;
Simple example to Save SAS log file:
The below example demonstrates how to save SAS log into an external file. In this code we create a new data set Whitefish by using the sashelp.fish data set.
The NEW keyword replaces the SampleLog.txt file every time when you execute the code and store log from the last execution.
Instead of adding file name with file location directly, you can use fileref with FILENAME. It simplifies your program especially when you’re importing and exporting multiple files.
filename fileref '/home/u61950255/Files/SampleLog.txt';
proc printto log=fileref new;
run;
data work.Whitefish;
set sashelp.fish;
where Species="Whitefish";
run;
proc print data=Whitefish;
run;
proc printto;
run;
You can see here after successfully execution of above program SAS print very limited information because everything has been sent to external file already.
This is how external log file (SampleLog.txt) looks like:
PS: The log file SampleLog.txt has all the information which is supposed to be printed in the LOG window. The proc print statement generates output in the OUTPUT window hence it won’t be included in the log file.
Save SAS OUTPUT Data into an External File
If your requirement is to save output data into an external file then it’s still possible with the proc printto procedure. You just need to use the PRINT option (instead of LOG option) along with the proc printto procedure.
SAS System options:
With the SAS system options you can format your external file. For example you can remove date and time, set line size, line size, etc.
options nodate pageno=1 linesize=80 pagesize=60 source;
- The NODATE option suppresses the display of the date and time in the output. PAGENO= specifies the starting page number.
- LINESIZE= specifies the output line length, and
- PAGESIZE= specifies the number of lines on an output page.
The SOURCE option writes lines of source code to the default destination for the SAS log.
Example
/* Save OUTPUT into an external file */
options nodate pageno=1 linesize=80 pagesize=60 source;
filename fileref '/home/u61950255/Files/SampleLog.txt';
proc printto print=fileref new;
run;
data work.Whitefish;
set sashelp.fish;
where Species="Whitefish";
run;
proc print data=Whitefish;
title 'Listing of Whitefish Data Set';
run;
proc printto;
run;
An external file looks like this:
FAQ
It’s absolutely possible to format SAS log file generated using PROC PRINTTO procedure. You can use SAS system options to achieve this. The most common options people use to format output files are:
options nodate pageno=1 linesize=80 pagesize=60 source;
There are multiple ways you can generate log file in SAS but the PROC PRINTTO procedure is the most commonly used among the SAS experts.
The PRINTTO procedure defines destinations for SAS procedure output and for the SAS log. You can store the SAS log or procedure output in an external file or in a SAS catalog entry.
You can use the PROC PRINTTO procedure with PRINT= option to generate external file which has SAS output information.
Syntax:
proc printto print='log-file' new;
run;