Help using Base SAS procedures

SAS Email if Errors

Reply
Frequent Contributor
Posts: 117

SAS Email if Errors

Hi Guys,

I am wondering if there is any code I can use / incorporate into my sas program which will email me at a specific email address if errors occur or if the program fails?

Thanks!

Regular Contributor
Posts: 233

SAS Email if Errors

filename mailbox email 'xxxxx@xxxx.com'

subject = 'test';

data _null_;

set table;

if <condition> then put "error";

call sleep(1,1);

run;

** this code will prevent you running the code further in case if the condition fails **;

data table2;

set table;

if <condition> then abort abend 123;

run;

Frequent Contributor
Posts: 117

SAS Email if Errors

So if I use this :

filename mailbox email 'xxx@xxx.com'

subject = 'test';

data _null_;

set table;

if <condition> then put "error";

call sleep(1,1);

run;

It will email me if any errors occur? and if I replace the data part with this:

** this code will prevent you running the code further in case if the condition fails **;

data table2;

set table;

if <condition> then abort abend 123;

run;

Then it will email and stop program?

Regular Contributor
Posts: 233

SAS Email if Errors

Yes. The below code is works as follows.

data test;

input col1 col2 col3;

datalines;

1 2 3

2 3 5

3 4 4

;

run;

condition= col1+col2 = column3

You will receive an email with subject = test and message as error. Are you looking for email if there is error in sas log?

filename mailbox email 'xxx@xxx.com'

subject = 'test';

data _null_;

set table;

if  col1+col2 ne column3  then put "error";

call sleep(1,1);

run;

Regular Contributor
Posts: 233

SAS Email if Errors

If you want to receive an email if an error occurs in SAS log, please gp through the link below.

http://www2.sas.com/proceedings/sugi31/128-31.pdf

Frequent Contributor
Posts: 117

SAS Email if Errors

Yes I am looking for this type of functionalty. It seems like the PDF explains a way to check a log file.

Is there any ways this can occur within the SAS program? So as soon as an error shows up in the run process...sas emails and notifies?

Regular Contributor
Posts: 233

SAS Email if Errors

you probably have to have code check points. I normally know where errors might occur in the code and I create check points. Below is an example.

data test;

input col1 col2 col3;

datalines;

1 2 3

2 3 5

3 4 4

;

run;

condition= col1+col2 = column3

filename mailbox email 'xxx@xxx.com'

subject = 'test';

data _null_;

set table;

if  col1+col2 ne column3  then put "error";

call sleep(1,1);

run;

PROC Star
Posts: 7,416

SAS Email if Errors

That is easy to do if you put the code in a macro and use a %if on the system error code to determine whether or not to send the email.

I don't have time to write it this morning as I'm in the middle of preparing a paper for SGF.  However, that paper includes a section that does something similar (sort of) in that it checks whether a date is a holiday and, if it is, sends an email.

I'll paste that macro here, but you would have to modify it yourself.  Obviously, you don't want to use it, as is, as it sends a picture of a menorah:

%macro sendit(to,subj,from,test=NO);

  %if &test. eq YES %then

    %let today=%sysevalf("12DEC2012"d);

  %else %let today=%sysfunc(today());

  %if %sysfunc(intindex(JewishHolidays,&today.)) %then %do;

    options EMAILSYS=SMTP

            EMAILID="your_email_address"

            EMAILPW="your_email_password"

            EMAILHOST="your_email_smtp_address"

            EMAILAUTHPROTOCOL=LOGIN

            EMAILPORT=587 ;

   /* Note: EMAILPORT may have to set to 25 */

 

    filename mymail email lrecl=256 TYPE='TEXT/HTML';

 

    data _null_;

      file mymail to=("&to.") subject="&subj.";

      array candles[17] $40. (17*' ');

      put "<BR>";

      put "<B><font face='courier new'>";

      x=intindex('JewishHolidays',&today.);

      msg=cat('Hope you have a Happy ',

       propcase(strip(put(x,s2jhol.))),'!');

      put msg "<BR> <BR>";

      if put(x,s2jhol.) eq 'HANUKKAH' then do;

        ncandles=2*(&today.-intnx('JewishHolidays',

         &today.,0))+1;

        if ncandles gt 7 then ncandles+2;

        candles[9]=")";

        msg=catx(' ',of %quote(candlesSmiley Happy);

        put "<font face='courier new' color=red>";

        put msg "<BR>";

        do i=1 to ncandles by 2;

          candles=")";

          if i eq 7 then i=9;

        end;

        put "<font face='courier new' color=red>";

        msg=catx(' ',of %quote(candles1-candles8));

        put msg;

        put "<font face='courier new' color=black>";

        put %quote(candles(9));

        put "<font face='courier new' color=red>";

        msg=catx(' ',of %quote(candles10-candles17));

        put msg "<BR>";

        do i=1 to 17 by 2;

          candles="|";

        end;

        msg=catx(' ',of %quote(candlesSmiley Happy);

        put "<font face='courier new' color=black>";

        put msg "<BR>";

        do i=2 to 16 by 2;

          candles="_";

        end;

        msg=catx(' ',of %quote(candlesSmiley Happy);

        put msg "<BR>";

        do i=1 to 17;

          if i eq 8 then i=11;

          candles=' ';

        end;

        msg=catx(' ',of %quote(candlesSmiley Happy);

        put msg "<BR>";

      end;

      put "</B></font><BR>";

      put "&from";

      put '</style></body></html>';

    run;

  %end;

%mend;

/* Example usage */

%sendit(EmailAddress,Happy Holidays,YourName,test=YES) /* If only testing */

%sendit(EmailAddress,Happy Holidays,YourName) /* Actual use */

Frequent Contributor
Posts: 117

Re: SAS Email if Errors

Really neat!

I will try to work with this but if someone has a working sample they use, please do share Smiley Happy

Contributor
Posts: 37

Re: SAS Email if Errors

I just ran across the following sample code for sending an automatic email if an error/warning is encountered: http://support.sas.com/kb/36/567.html.

Valued Guide
Posts: 3,208

Re: SAS Email if Errors

Michelle just a question: What will be the result if all processing has stopped for a reason?
Thinking the other way could be better. The signalling of an "ok" processing is more certain event. That is functionality in schedulers, when something should be finished but is not in time that is the signal. 

---->-- ja karman --<-----
Contributor
Posts: 37

Re: SAS Email if Errors

Good point! However, I want to receive the error/warning email because sometimes the program runs and spits out results even though something may have gone wrong somewhere.  So, maybe it is better to apply both approaches and have an 'ok' email, in addition to the 'error' email.

PROC Star
Posts: 1,269

Re: SAS Email if Errors

Good point Michelle.

Suppose you have 3 different programs that you expect to run successfully every night.

Recently I've been playing with an approach that is like:

1.  When each program runs, at the end of the program it calls a %logscan() macro which checks the log for any errors/warnings/bad notes.  It then appends a single record to a "joblog" dataset which has: ProgramName, RunDate, LogCheckResult.

2.  Then there is a separate program scheduled to run nightly which looks at the data in the joblogdataset.  It expects to find records in the joblog dataset for ProgramA, ProgramB, and ProgramC all run within the past 24 hours, and expects the LogChekResult to be "Pass" for each job.  It sends me a single email every night, which says "Nightly check passed" or "Nightly check failed" (because I job had a bad log or a job did not run), with the body of the email listing the jobs that have run.

--Q.

Super User
Posts: 7,100

Re: SAS Email if Errors

Our solution is this:

- all SAS batch jobs MUST exit with return code zero if they finish "logically OK". This of course requires solid coding

- there is a sas batch wrapper script that is called by the scheduler, pertinent information is passed via environment variables

- if the SAS execution itself finishes with zero, the log is checked (via grep) for the existence of certain words/phrases that indicate faulty execution that did not trigger a SAS exit code <> 0 (for instance there are certain conditions that can happen when FTPing from z/OS, or a single line of faulty data in an infile). Such phrases may cause (limited) reruns and/or the setting of special exit codes.

- if after all that the exit code is <> 0, the log is displayed to stdout, which the scheduler captures to its own log

- some programs send email (if execution was successful) on their own via the server OS sendmail, and of course the scheduler has "send mail" options (and will always do so in case of a nonzero exit code)

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Valued Guide
Posts: 3,208

Re: SAS Email if Errors

That is leaving the last question. Why should you build/develop something that is already solved and part of a service.
- Schedulers for organizing jobs/processes with feedback

- error detection and messaging with the SAS environment it self.
Sometimes you are needing to learn some basics or the question and solution is that simple there is no need for additional investments.
As soon as you are going to develop a whole complicated dedicated system, normally it is: reuse something already existing, buy before build.
I know building it yourself is that nice, but is it always necessary?   

---->-- ja karman --<-----
Ask a Question
Discussion stats
  • 19 replies
  • 4295 views
  • 0 likes
  • 7 in conversation