SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
Sean_OConnor
Obsidian | Level 7

Folks,

I have developed a project for my processing of quiet an intensive workload.

I have broken it into 4 separate programs.

 

At the very top of my programs I tell SAS to print the log to a specific location.

proc printto log="location\\Parsing_&date..txt";
run;

At the end of the program I have.

proc printto;
run;

What I would like to do is have SAS check these logs and send me an email if it finds an error in them. I have seen this code online. I would like to augment the below to actually pull what the error is too. I would also like to add another condition to check if the code is still running.

So I would have condition 1 - An error occurred 2 - Code is still running 3 - The job ran to completion

Can anyone provide any advice?

   %macro send_mail;
   filename mymail email 'myemail' subject='Processing';
     %if &syscc>0 %then %do;
       data _null_;
         file mymail;
         put 'An ERROR has occurred in the code';
       run;
     %end;
     %else %do;
       data _null_;
         file mymail;
         put 'The job ran to completion';
       run;
     %end;
   %mend;
7 REPLIES 7
Sean_OConnor
Obsidian | Level 7

Kurt would this still work even if at the top of my program I'm printing the log to an external area??

Kurt_Bremser
Super User

The automatic macro variable SYSCC (follow the link) is independent from where the log is written. It contains the exit code that SAS will return to the parent process when it ends. For a run free of WARNINGs and ERRORs, this is 0.

Sean_OConnor
Obsidian | Level 7

I have a macro written that carries out a procedure n times. I'm printing all from the logs to an external file. 

 

I tried the following and I'm getting email saying an error has occurred. When I examine the log there is no error?

 

proc printto log="Logs\i..txt";
%macro agg(month,final_data);

*My macro steps;

%mend agg;
%agg(temps.apr_20,pmoda.apr_20);

   %macro send_mail;
   filename mymail email 'myemail' subject='Processing';
     %if &SYSCC>0 %then %do;
       data _null_;
         file mymail;
         put 'An ERROR has occurred in the code';
       run;
     %end;
     %else %do;
       data _null_;
         file mymail;
         put 'The job ran to completion';
       run;
     %end;
   %mend;

   %send_mail
proc printto;
run;


Kurt_Bremser
Super User

Insert

%put &syscc;

at the beginning of your code to see if you enter it in a clean state.

You can also do

%let syscc = 0;

at the beginning of your code to reset the macro variable if the non-zero code is caused by something in your autoexec. SYSCC is read and write.

ballardw
Super User

@Sean_OConnor wrote:

I have a macro written that carries out a procedure n times. I'm printing all from the logs to an external file. 

 

I tried the following and I'm getting email saying an error has occurred. When I examine the log there is no error?


The warning condition code in SAS sets &SYSCC to 4. So if you are getting any warnings the macro variable is not 0.
 
Depending on the content of your code you may need to make other changes.
When the ERRORCHECK= SAS system option is set at NORMAL, the value of SYSCC will be 0 even if an error exists in a LIBNAME or FILENAME statement, or in a LOCK statement in SAS/SHARE software. The value of SYSCC will also be 0 when the %INCLUDE statement fails due to a nonexistent file.
Quentin
Super User

If you want to have the email include the actual error in the body of the message, then I think you might want to parse the log, looking for errors, warnings, and bad notes.  It's a non-trivial task, but of course once you have a %logscan() macro that will email you, you can use it everywhere.  Also, there are plenty of %logscan macros in papers and blog posts etc. 

 

Here's one I like, and there are more mentioned in the comments:

https://bi-notes.com/sas-eg-check-the-log/

 

The other option to scanning the log is to just test &syscc, as you are doing, and when you send an email to notify you of an error, attach the log file to the email.  Or sometimes you can put a link to the log file in the body of the email, rather than attach it.  Even if you do the log scanning and put the error message in the email, nine times out of 10, you end up opening the full log file.

The Boston Area SAS Users Group is hosting free webinars!
Next up: Troy Martin Hughes presents Calling Open-Source Python Functions within SAS PROC FCMP: A Google Maps API Geocoding Adventure on Wednesday April 23.
Register now at https://www.basug.org/events.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1369 views
  • 1 like
  • 4 in conversation