BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

Hello

I want  to run a sas program and -

1-create a binary macro variable that get value 1 if program had error

2-create a binary macro variable that get value 1 if program had warning

 

send automatic email via sas to my email (Only in case of error or warning) and say one of these 3 options:

"Error occured  during run the code"

"Warning occured  during run the code"

"Error and Warning  occured  during run the code"

 

What is the way to do it?

 

For example lets say I run this code that has error

data cars;
set sashelp.cars;
Run;

proc sql;
create table want as
select  make,
        min(invoice) as min_invoice,
        max(invoice) as max_invoice,
		median(invoice) as median_invoice,
		count(disinct  type_ as distinct_types
from cars
group by make
;
quit;
8 REPLIES 8
Ksharp
Super User

1)Firstly save your sas code 's LOG  into a file.

proc printto file='c:\temp\x.log' new;
run;
/*.......your code here...........*/
proc printto;
run;

2)Secondly, parse this LOG file and check it is ERROR or WARNING :

%let error=no;
%let warning=no;


data _null_; infile 'c:\temp\x.log'; input; if _infile_ =: 'ERROR' then call symputx('error','yes'); if _infile_ =: 'WARNING' then call symputx('warning','yes'); run;

3)Thirdly, make a macro to send your e-mail or not.


%macro email;
data _null_;
file email;

%if &error.=yes %then %do; put 'there is an error.'; %end;
%else %if &warning.=yes %then %do; put 'there is an warning.'; %end;
%else %if &error.=yes and &warning.=yes  %then %do; put 'there is an error and warning .'; %end;

run;
%mend;

%email

 

 

Ronein
Onyx | Level 15

Thanks so much,

I have some questions please-

Here is the code I run based on your feedback

 

Where should I write my email address??? 

 

Should I located this code -

proc printto log="/usr/local/SAS/SASUsers/LabRet/UserDir/udclk79/RonLog.log" new;
run;

in top of program??

I run the coe and get error-

ERROR: Insufficient authorization to access /usr/local/SAS/Config/Lev1/LabRet/email.dat.

 

/* save your sas code 's LOG  into a file*/
proc printto log="/usr/local/SAS/SASUsers/LabRet/UserDir/udclk79/RonLog.log" new;
run;

/**My program code**/
data cars;
set sashelp.cars;
Run;
proc sql;
create table want as
select  make,
        min(invoice) as min_invoice,
        max(invoice) as max_invoice,
		median(invoice) as median_invoice,
		count(disinct  type_ as distinct_types
from cars
group by make
;
quit;



proc printto log=log;
run;

/*parse this LOG file and check it is ERROR or WARNING*/
%let error=no;
%let warning=no;
data _null_;
infile "/usr/local/SAS/SASUsers/LabRet/UserDir/udclk79/RonLog.log";
input;
if _infile_ =: 'ERROR' then call symputx('error','yes');
if _infile_ =: 'WARNING' then call symputx('warning','yes');
run;


/*make a macro to send your e-mail or not.*/
%macro email;
data _null_;
file email;
%if &error.=yes %then %do; put 'there is an error.'; %end;
%else %if &warning.=yes %then %do; put 'there is an warning.'; %end;
%else %if &error.=yes and &warning.=yes  %then %do; put 'there is an error and warning .'; %end;
run;
%mend;
%email

The log with the error-

1 The SAS System 14:21 Tuesday, November 18, 2025

1 ;*';*";*/;quit;run;
2 OPTIONS PAGENO=MIN;
3 %LET _CLIENTTASKLABEL='Program';
4 %LET _CLIENTPROCESSFLOWNAME='Standalone Not In Project';
5 %LET _CLIENTPROJECTPATH='';
6 %LET _CLIENTPROJECTPATHHOST='';
7 %LET _CLIENTPROJECTNAME='';
8 %LET _SASPROGRAMFILE='';
9 %LET _SASPROGRAMFILEHOST='';
10
11 ODS _ALL_ CLOSE;
12 OPTIONS DEV=SVG;
13 GOPTIONS XPIXELS=0 YPIXELS=0;
14 %macro HTML5AccessibleGraphSupported;
15 %if %_SAS_VERCOMP_FV(9,4,4, 0,0,0) >= 0 %then ACCESSIBLE_GRAPH;
16 %mend;
17 FILENAME EGHTML TEMP;
18 ODS HTML5(ID=EGHTML) FILE=EGHTML
19 OPTIONS(BITMAP_MODE='INLINE')
20 %HTML5AccessibleGraphSupported
21 ENCODING='utf-8'
22 STYLE=HTMLBlue
23 NOGTITLE
24 NOGFOOTNOTE
25 GPATH=&sasworklocation
26 ;
NOTE: Writing HTML5(EGHTML) Body file: EGHTML
27
28 /* save your sas code 's LOG into a file*/
29 proc printto log="/usr/local/SAS/SASUsers/LabRet/UserDir/udclk79/RonLog.log" new;
30 run;

NOTE: PROCEDURE PRINTTO used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

49
50 /*parse this LOG file and check it is ERROR or WARNING*/
51 %let error=no;
52 %let warning=no;
53 data _null_;
54 infile "/usr/local/SAS/SASUsers/LabRet/UserDir/udclk79/RonLog.log";
55 input;
56 if _infile_ =: 'ERROR' then call symputx('error','yes');
57 if _infile_ =: 'WARNING' then call symputx('warning','yes');
58 run;

NOTE: The infile "/usr/local/SAS/SASUsers/LabRet/UserDir/udclk79/RonLog.log" is:
Filename=/usr/local/SAS/SASUsers/LabRet/UserDir/udclk79/RonLog.log,
Owner Name=udclk79,
Group Name=gr_il_sas_users_labret,
Access Permission=-rw-r--r--,
Last Modified=18Nov2025:14:35:46,
File Size (bytes)=1902

NOTE: 53 records were read from the infile "/usr/local/SAS/SASUsers/LabRet/UserDir/udclk79/RonLog.log".
The minimum record length was 0.
The maximum record length was 133.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

59
3 The SAS System 14:21 Tuesday, November 18, 2025

60
61 /*make a macro to send your e-mail or not.*/
62 %macro email;
63 data _null_;
64 file email;
65 %if &error.=yes %then %do; put 'there is an error.'; %end;
66 %else %if &warning.=yes %then %do; put 'there is an warning.'; %end;
67 %else %if &error.=yes and &warning.=yes %then %do; put 'there is an error and warning .'; %end;
68 run;
69 %mend;
70 %email

ERROR: Insufficient authorization to access /usr/local/SAS/Config/Lev1/LabRet/email.dat.
NOTE: The SAS System stopped processing this step because of errors.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

71
72 %LET _CLIENTTASKLABEL=;
73 %LET _CLIENTPROCESSFLOWNAME=;
74 %LET _CLIENTPROJECTPATH=;
75 %LET _CLIENTPROJECTPATHHOST=;
76 %LET _CLIENTPROJECTNAME=;
77 %LET _SASPROGRAMFILE=;
78 %LET _SASPROGRAMFILEHOST=;
79
80 ;*';*";*/;quit;run;
81 ODS _ALL_ CLOSE;
82
83
84 QUIT; RUN;
85

Kathryn_SAS
SAS Employee

The following SAS Note has an example:

https://support.sas.com/kb/32/187.html

 

Ronein
Onyx | Level 15

I  saw this example and it is working 100%

In this example ther is no action of  saving Log file .

What is the purpose of saving the Log file? 

Did you recommend to add the Log file as attach file to the email?

Or print the Log file in the email body??(Not good if log is very big)

 

   data a;
     input x $ y;
   cards;
   a 1
   b 2
   c 3
   ;
 Run;


proc means sum;
var x y;
run;


%macro send_mail;
   filename mymail email to=('xxx.xxx@xxx.xxx.xxx') subject='סיכום אם הייתה שגיאה בריצה'  encoding='utf-8';
     %if &syscc>0 %then %do;
       data _null_;
         file mymail;
         put 'התרחשה שגיאה בהרצה של תוכנית סאס';
       run;
     %end;
     %else %do;
       data _null_;
         file mymail;
         put 'הריצה בוצעה ללא שגיאות';
       run;
     %end;
     %let syscc=0;
   %mend;
   %send_mail

 

Kathryn_SAS
SAS Employee

This example checks the value of the &syscc macro variable at a step boundary. You could save the log file if you want to see what the actual log message is. Also since each step boundary generates its own &syscc value, you could use the log to check for multiple &syscc values.

Kurt_Bremser
Super User

What do you mean by "run a sas program"?

  • Submit code in Studio or EG
  • Submit a whole project in EG or flow in Studio
  • Run a separately saved (.sas file) program in batch mode

And do you want to send the notification from within the program/submitted code, or from somewhere "outside" (e.g. shell script)?

Ksharp
Super User

You should put your code of sending email in macro %email. My code is just an example:

 

%let log=c:\temp\log.log ; */usr/local/SAS/SASUsers/LabRet/UserDir/udclk79/RonLog.log;

/* save your sas code 's LOG  into a file*/
proc printto log="&log." new;
run;

/**My program code**/
data cars;
set sashelp.cars;
Run;
proc sql;
create table want as
select  make,
        min(invoice) as min_invoice,
        max(invoice) as max_invoice,
		median(invoice) as median_invoice,
		count(disinct  type_ as distinct_types
from cars
group by make
;
quit;

proc printto ;
run;




/*parse this LOG file and check it is ERROR or WARNING*/
%let error=no;
%let warning=no;
data _null_;
infile "&log.";
input;
if _infile_ =: 'ERROR' then call symputx('error','yes');
if _infile_ =: 'WARNING' then call symputx('warning','yes');
run;

/*Check macro variable ERROR and WARNING*/
%put &=error. &=warning. ;






%macro email;

/****SEND EMAIL****/
/****SEND EMAIL****/
/****SEND EMAIL****/
/****SEND EMAIL****/
FILENAME mail EMAIL 
from="XXX.EXXX@XXXX"
TO=("XXX.EXXX@XXXX")
SUBJECT=" --&last_mon_in_Report_YYMM1.-בקרת ציון כא-דוח לפני פרסום סקור"
encoding='utf-8'  /**To show Hebrew!!!**/
CONTENT_TYPE="text/plain" 
attach=(&path. content_type="excel");
ODS LISTING CLOSE;
/*ODS HTML BODY=mail;*/

/*
data _null_;
file mail;
put 'היי';
put 'מצורף קובץ בקרה -&last_mon_in_Report_YYMM1.';
put ' ';
put 'בברכה, XXX XXX';
run;
*/

data _null_;
file mail;

%if &error.=yes %then %do; put 'there is an error.'; %end;
%if &warning.=yes %then %do; put 'there is an warning.'; %end;

run;
%mend;

%email

 

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
  • 8 replies
  • 341 views
  • 0 likes
  • 4 in conversation