Hi,
I have a code to check errors in sas log and send it out by email, but I don't want sas to send the email when the program executes successfully, I tried to put an if then statement taking into account the value resulted from the automatic macro variable syserr but it's not working, also I have tried with abort statement but this one write an error in sas log.
%let check = &syserr;
SAS CODE here
data _null_;
if &check=0 then do;
stop;
end;
run;
filename mylog "/sasdata/user_Data/log.log";
filename report "/sasdata/user_Data/reportlog.txt" ;
data _null_;
if &check > 0 then do;
infile mylog;
input;
lineno+1; /* Increment Line Number */
file report;
rec = _infile_;
/* Check for ERROR: type error message */
foundit = index(rec,"ERROR:");
if ( foundit > 0 ) then do;
put lineno " " rec;
end;
/* Check for ERROR 99-999: type error message */
foundit = index(rec,"ERROR ");
if ( foundit > 0 ) then do;
rec=substr(rec,foundit);
if length(rec) > 15 then do;
tempstr=substr(rec,1,15);
if (substr(tempstr,7,1) >= "0" and substr(tempstr,7,1) <= "9"
and index(tempstr,"-") > 0 and index(tempstr,":") > 0 ) then do;
put lineno " " rec;
end;
end;
end;
/* Check for WARNING: type warning message */
foundit = index(rec,"WARNING:");
if ( foundit > 0 ) then do;
put lineno " " rec;
end;
/* Check for WARNING 99-999: type error message */
foundit = index(rec,"WARNING ");
if ( foundit > 0 ) then do;
rec=substr(rec,foundit);
if length(rec) > 15 then do;
tempstr=substr(rec,1,15);
if (substr(tempstr,9,1) >= "0" and substr(tempstr,9,1) <= "9"
and index(tempstr,"-") > 0 and index(tempstr,":") > 0 ) then do;
put lineno " " rec;
end;
end;
end;
/* Check for "ERROR [" type warning message */
foundit = index(rec,"ERROR [");
if ( foundit > 0 ) then do;
put lineno " " rec;
end;
/* Check for "WARN [" type warning message */
foundit = index(rec,"WARN [");
if ( foundit > 0 ) then do;
put lineno " " rec;
end;
end;
filename outbox email attach=("/sasdata/user_Data/reportlog.txt")
to=''
type='text/html'
subject= "Log ETL"
from='';
run;
run;
Rather than looking af the SYSERR Automatic variable, I would use the SYSCC variable.
It has two advantages:
So I would start with
%let SYSCC=0;
SAS CODE here
%let check=&SYSCC;
Then, after the SAS code submitted, &SYSCC contains the maximum error level encountered, which you save in your CHECK variable.
In which case you may want to change the "if &check>0 then do;" in your log gathering datastep to "if &check>4 then do;" as level 4 is just a Warning, and apparently you do not want to report those.
I do not see how you send your mail, allocating the EMAIL file is not enough. So I presume that you have a datastep in the end to send the mail. If things are OK, and you do not want to send the mail, just put this in your datastep:
Data _null_;
file outbox <options>;
if &check<=4 then do;
put "!EM_ABORT!";
stop;
end;
The !EM_ABORT! message tells SAS not to send the mail after all.
This stopped sas from processing the next data step and didn't sent the email but an error is written to the log, I want the program to stop without error.
%macro condition;
%let check = &syserr;
%if &check=0 %then %do;
%abort cancel;
%end;
%mend;
%condition;
data _null_;
%macro condition;
%if &syserr=0 %then %do;
%abort cancel;
%end;
%mend;
%condition;
run;
The error is:
ERROR: Execution canceled by an %ABORT CANCEL statement.
I put the full code inside the macro but now the email is not being sent when an error occur.
Here is the full code:
%macro condition;
proc printto log="/sasdata/user_Data/log.log" new;
run;
filename mylog1 "/sasdata/user_Data/log.log";
PROC SQL;
CREATE TABLE SUMMARY AS
SELECT FECHA,
CIF,
T1.'CLIENTES SEGUN ICBS'N,
FROM WORK.TRANSP_EFE T1;
QUIT;
%put &syserr;
proc printto log=log;
run;
data _null_;
infile mylog1 ;
input;
put _infile_;
run;
%if &syserr > 0 %then %do;
filename mylog "/sasdata/user_Data/log.log";
filename report "/sasdata/user_Data/reportlog.txt" ;
data _null_;
infile mylog;
input;
lineno+1; /* Increment Line Number */
file report;
rec = _infile_;
/* Check for ERROR: type error message */
foundit = index(rec,"ERROR:");
if ( foundit > 0 ) then do;
put lineno " " rec;
end;
/* Check for ERROR 99-999: type error message */
foundit = index(rec,"ERROR ");
if ( foundit > 0 ) then do;
rec=substr(rec,foundit);
if length(rec) > 15 then do;
tempstr=substr(rec,1,15);
if (substr(tempstr,7,1) >= "0" and substr(tempstr,7,1) <= "9"
and index(tempstr,"-") > 0 and index(tempstr,":") > 0 ) then do;
put lineno " " rec;
end;
end;
end;
/* Check for WARNING: type warning message */
foundit = index(rec,"WARNING:");
if ( foundit > 0 ) then do;
put lineno " " rec;
end;
/* Check for WARNING 99-999: type error message */
foundit = index(rec,"WARNING ");
if ( foundit > 0 ) then do;
rec=substr(rec,foundit);
if length(rec) > 15 then do;
tempstr=substr(rec,1,15);
if (substr(tempstr,9,1) >= "0" and substr(tempstr,9,1) <= "9"
and index(tempstr,"-") > 0 and index(tempstr,":") > 0 ) then do;
put lineno " " rec;
end;
end;
end;
/* Check for "ERROR [" type warning message */
foundit = index(rec,"ERROR [");
if ( foundit > 0 ) then do;
put lineno " " rec;
end;
/* Check for "WARN [" type warning message */
foundit = index(rec,"WARN [");
if ( foundit > 0 ) then do;
put lineno " " rec;
end;
filename outbox email attach=("/sasdata/user_Data/reportlog.txt")
to=''
type='text/html'
subject= "Log ETL"
from='';
run;
run;
%end;
%mend;
%condition;
This is the full log:
1 The SAS System 14:17 Thursday, November 22, 2018
1 ;*';*";*/;quit;run;
2 OPTIONS PAGENO=MIN;
3 %LET SYSLAST=WORK.TRANSP_EFE;
4 %LET _CLIENTTASKLABEL='Program1';
5 %LET _CLIENTPROJECTPATH='C:\Users\U\Documents\SASEG_SOLEMPRESARIALES\ProjectLOGS.egp';
6 %LET _CLIENTPROJECTNAME='ProjectLOGS.egp';
7 %LET _SASPROGRAMFILE=;
8
9 ODS _ALL_ CLOSE;
10 OPTIONS DEV=ACTIVEX;
11 GOPTIONS XPIXELS=0 YPIXELS=0;
12 FILENAME EGSR TEMP;
13 ODS tagsets.sasreport13(ID=EGSR) FILE=EGSR
14 STYLE=HtmlBlue
15 STYLESHEET=(URL="file:///C:/Program%20Files/SASHome/SASEnterpriseGuide/6.1/Styles/HtmlBlue.css")
16 NOGTITLE
17 NOGFOOTNOTE
18 GPATH=&sasworklocation
19 ENCODING=UTF8
20 options(rolap="on")
21 ;
NOTE: Writing TAGSETS.SASREPORT13(EGSR) Body file: EGSR
22
23 GOPTIONS ACCESSIBLE;
24 %macro condition;
25
26 proc printto log="/sasdata/u_Data/log.log" new;
27 run;
28
29 filename mylog1 "/sasdata/u_Data/log.log";
30
31 PROC SQL;
32 CREATE TABLE SUMMARY AS
33 SELECT FECHA,
34 CIF,
35 T1.'CLIENTES SEGUN ICBS'N,
36 FROM WORK.TRANSP_EFE T1;
37 QUIT;
38
39 %put &syserr;
40
41 proc printto log=log;
42 run;
43
44 data _null_;
45 infile mylog1 ;
46 input;
47 put _infile_;
48 run;
49
50
51 %if &syserr > 0 %then %do;
52
53 filename mylog "/sasdata/u_Data/log.log"; /* <=== Specify your SAS log location here. */
54
55 filename report "/sasdata/u_Data/reportlog.txt" ; /* <=== Specify the location for your report here. */
56
57
2 The SAS System 14:17 Thursday, November 22, 2018
58 data _null_;
59
60
61 infile mylog;
62 input;
63
64 lineno+1; /* Increment Line Number */
65
66 file report;
67
68 rec = _infile_;
69
70 /* Check for ERROR: type error message */
71
72 foundit = index(rec,"ERROR:");
74 if ( foundit > 0 ) then do;
75
76 put lineno " " rec;
77 end;
78
79 /* Check for ERROR 99-999: type error message */
81 foundit = index(rec,"ERROR ");
83 if ( foundit > 0 ) then do;
84
85 rec=substr(rec,foundit);
86
87 if length(rec) > 15 then do;
88
89 tempstr=substr(rec,1,15);
90
91 if (substr(tempstr,7,1) >= "0" and substr(tempstr,7,1) <= "9"
92 and index(tempstr,"-") > 0 and index(tempstr,":") > 0 ) then do;
93
94 put lineno " " rec;
95 end;
96 end;
97 end;
98
99 /* Check for WARNING: type warning message */
100
101 foundit = index(rec,"WARNING:");
103 if ( foundit > 0 ) then do;
104
105 put lineno " " rec;
106 end;
107
108 /* Check for WARNING 99-999: type error message */
110 foundit = index(rec,"WARNING ");
112 if ( foundit > 0 ) then do;
113
114 rec=substr(rec,foundit);
115
116 if length(rec) > 15 then do;
117
118 tempstr=substr(rec,1,15);
119
120 if (substr(tempstr,9,1) >= "0" and substr(tempstr,9,1) <= "9"
121 and index(tempstr,"-") > 0 and index(tempstr,":") > 0 ) then do;
3 The SAS System 14:17 Thursday, November 22, 2018
122
123 put lineno " " rec;
124 end;
125 end;
126 end;
127
128 /* Check for "ERROR [" type warning message */
129
130 foundit = index(rec,"ERROR [");
132 if ( foundit > 0 ) then do;
133
134 put lineno " " rec;
135 end;
136
137 /* Check for "WARN [" type warning message */
138
139 foundit = index(rec,"WARN [");
141 if ( foundit > 0 ) then do;
142
143 put lineno " " rec;
144 end;
145
146 filename outbox email attach=("/sasdata/u_Data/reportlog.txt")
147
148 to=''
149
150 type='text/html'
151 subject= "Log ETL"
152 from='';
153
161
162 run;
163 run;
164
165 %end;
166 %mend;
167 %condition;
NOTE: PROCEDURE PRINTTO used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
NOTE: The infile MYLOG1 is:
Filename=/sasdata/u24807_Data/log.log,
Owner Name=U40621,Group Name=sas,
Access Permission=rw-rw-r--,
Last Modified=Thu Nov 22 16:51:02 2018,
File Size (bytes)=1181
NOTE: PROCEDURE PRINTTO used (Total process time):
real time 0.00 seconds
cpu time 0.01 seconds
22: LINE and COLUMN cannot be determined.
NOTE 242-205: NOSPOOL is on. Rerunning with OPTION SPOOL might allow recovery of the LINE and COLUMN where the error has occurred.
ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, (, *, **, +, ',', -, /, <, <=, <>, =, >, >=, ?, AND, BETWEEN,
CONTAINS, EQ, EQT, FROM, GE, GET, GT, GTT, LE, LET, LIKE, LT, LTT, NE, NET, OR, ^=, |, ||, ~=.
79: LINE and COLUMN cannot be determined.
NOTE: NOSPOOL is on. Rerunning with OPTION SPOOL might allow recovery of the LINE and COLUMN where the error has occurred.
ERROR 79-322: Expecting a FROM.
NOTE: PROC SQL set option NOEXEC and will continue to check the syntax of statements.
4 The SAS System 14:17 Thursday, November 22, 201
8
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE SQL used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
1012
NOTE: 24 records were read from the infile MYLOG1.
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
168
169
170
171
172
173 GOPTIONS NOACCESSIBLE;
5 The SAS System 14:17 Thursday, November 22, 2018
174 %LET _CLIENTTASKLABEL=;
175 %LET _CLIENTPROJECTPATH=;
176 %LET _CLIENTPROJECTNAME=;
177 %LET _SASPROGRAMFILE=;
178
179 ;*';*";*/;quit;run;
180 ODS _ALL_ CLOSE;
181
182
183 QUIT; RUN;
184
syserr value is 1012
This is the result when I ran with symbolgen and mfile options:
Why the syserr resolves to 1012 and then to 0?
real time 0.00 seconds
cpu time 0.00 seconds
SYMBOLGEN: Macro variable SYSERR resolves to 1012
1012
NOTE: 33 records were read from the infile MYLOG1.
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
SYMBOLGEN: Macro variable SYSERR resolves to 0
Personally I think a much more reliable way to deliver what you want would be to use a server-based batch scheduler like LSF then:
The big advantage of this approach is you don't need to check for any errors in your main program as the scheduler does that. If you have the SAS option SYNTAXCHECK switched on (this is the default condition for batch jobs) then your main program stops doing any processing of data as soon as it hits an error - pretty much what you want with no coding!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.