How to e-mail SAS Log files

Did you ever want the log file from a SAS batch job to be saved with the date and time that the job ran and/or be e-mailed to a recipient or group of recipients. At least for Windows/SAS environments, here's the secret below:

Step 1

Set up a SAS program that gets called from a batch file that gets called from each batch file that runs a SAS program. It should look something like the following:

/* Set two SAS macro variables from environment variables */

%let sasmail=%sysget(sasmail);

%let saslog=%sysget(saslog);

Filename OUTBOX EMAIL ' ' ; /* Set up filename for email */

Data _null_ ;

File OUTBOX /* Use Filename defined above */

TO=(&sasmail) /* Get recipient address */

Attach=("&saslog") /* Attach the log file */

Subject="Log file &saslog"; /* Add a subject */

Put "SAS log file is attached" ; /* Goes in the body */

Run;

Step 2

Set up a batch file to call the above SAS program. It should look something like the one below:

rem Put actual email address of recipeint(s) below
set sasmail="you@email.com" "another@email.com"

rem Copy the original sas log to one that has the date and time appended (for future reference)
copy "%saslog%"
"%saslog%%date:~-4,4%-%date:~-10,2%-%date:~-7,2%-%time:~-11,2%%time:~-8,2%%time:~-5,2%.log"

rem Call the sas process that emails the above log as an attachemnt to the email address(ees) above.
"D:\SAS\SAS 9.1\sas.exe" "F:\SAS ETL Jobs\sendemail.sas" -config "D:\SAS\SAS 9.1\nls\en\SASV9.CFG"
-log "F:\SAS ETL Jobs\logs\sendemail.log"

Step 3

In each of the SAS bat files that call .sas programs, insert two lines:

  1. One to set an environment variable for the name of the log file.
  2. One to call the bat file from step 2 that processes the log files.

An example follows:

 rem This process run the SAS process in batch suitable for scheduling

set saslog=F:\SAS ETL Jobs\logs\SasProgram_ETL.log

"D:\SAS\SAS 9.1\sas.exe" "F:\SAS ETL Jobs\SasProgram_ETL.sas" -config "D:\SAS\SAS 9.1\nls\en\SASV9.CFG" -log "%saslog%"

call "F:\SAS ETL Jobs\ProcessLog.bat"

That's it. Enjoy