is there any code/macro I can incorporate into my sas program that will email me as soon as an error occurs in my sas code while it is running?
Also is it possible for this email to contain the error that happened?
is there any code/macro I can incorporate into my sas program that will email me as soon as an error occurs in my sas code while it is running?
Also is it possible for this email to contain the error that happened?
Yes.... and no...
This is possible - but there's no nice way to do it. You would have to check after each procedure to see if an error occurred. Basically you would insert a line of code to perform the test dozens (or hundreds) of times throughout your code.
I often find that it's sufficient to perform a test after the entire program has run. If an error did occur halfway through then SAS will usually enter syntax check mode anyway so it won't execute any code after the error.
This approach also comes with it's own separate set of problems. To start with the SYS macro variables that contain information about errors only store info for the most recent error. We could check the log, but the problem with this is that the log is still in use by the program we are currently running and to examine which prevents us from using SAS to open it to read from it.
The way I get around this is to call SAS twice. The first time to run the program and save the log to a specified file. The second time to run a program that will check the logfile that was just created and to send an email if specific criteria are met (such as the line begins with
ERROR:
.Some details... when you launch sas the 2nd time, you can use the
SYSPARM
parameter to pass in the name of the logfile you want to check. You can parse the logfile with code something like this (I suggest you customize the conditions to your own situation):It's not clear from your question whether you know how to send an email in SAS, but if not I suggest googling it first and if you still can't get it working then come back and post a separate question.
EDITS: You can specify where SAS should save the logfile to by using the
-LOG "myfile.log"
parameter when you call SAS.This concise article gives simple SAS code which shows how to check for errors and send an email in an error condition. As Rob already mentioned, you should check for error codes after each procedure.
https://heuristically.wordpress.com/2012/02/09/return-codes-errors-sas/
This code doesn't send the log, but you could send a general error by defining a macro variable with a brief description like "failed to query the SQL database." At our organization we run about 50-100 (?) SAS programs in batch jobs, and we save each log with a date stamp like 2012-03-05 13:05 daily job.log