Hello,
How can I get the information that a running program failed ?
by doing that, my purpose is (for example) to send a mail to inform
thanks in advance for your help.
regards
Nasser
Sure. Of course.
You can scan all these LOG file and output the row started with ERROR or WARN.
And attach the log file name together.
%let path= c:\temp\log ; * the path stored LOG files;
filename x "&path";
data log_file;
did=dopen("x");
do i=1 to dnum(did);
file=dread(did,i);
if lowcase(scan(file,-1,'.'))='log' then do;
log_file=cats("&path",'\',file);
output;
end;
end;
keep log_file;
run;
data want;
set log_file;
length fname filename $ 200;
infile dummy filevar=log_file filename=fname end=last length=len;
filename=fname;
do while(not last);
input log $varying200. len;
if left(log) in: ('ERROR' 'WARN') then output;
end;
run;
First, you need to clearly define "failed".
For me, this was (in the first place) a return code of non-zero, which you can check from the scheduler or in-program from &SYSCC.
sorry, by "failed", I mean, the launched program stops because of error.
%macro erreur_programme; /* trap error here */ %if &syserr^=0 %then %do; %put an error occurs ; %end ; %mend erreur_programme ; data test; x=1 /* <-- intentionally leave off a semicolon to produce an error*/ run ; %erreur_programme;
I try this code but it does not work
Macro timing !!!
Since your DATA step was never completed (the run keyword is part of the incomplete assignment statement), the macro is resolved while the data step is being compiled.
Look at the log:
(run in SAS on Demand)
81 data test; 82 x=1 /* <-- intentionally leave off a semicolon to produce an error*/ 83 run ; ___ 22 ERROR 22-322: Syntaxfehler, erwartet wird eines der folgenden: !, !!, &, *, **, +, -, /, <, <=, <>, =, >, ><, >=, AND, EQ, GE, GT, IN, LE, LT, MAX, MIN, NE, NG, NL, NOTIN, OR, ^=, |, ||, ~=. 84 85 %erreur_programme; 86 87 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK; WARNING: The data set WORK.TEST may be incomplete. When this step was stopped there were 0 observations and 2 variables. WARNING: Datei WORK.TEST wurde nicht ersetzt, da da dieser Schritt angehalten wurde.
The WARNINGs coming from the DATA step appear after your macro call.
You must make sure that a previous step has been completed, e.g. like this:
%macro erreur_programme;
;run;quit;
/* trap error here */
%if &syserr^=0 %then %do;
%put an error occurs ;
%end ;
%mend erreur_programme;
I still recommend to use SYSCC because of this:
%put SYSERR = &syserr.;
%put SYSCC = &syscc.;
data test;
x = 1
run;
;run; /* end step */
%put SYSERR = &syserr.;
%put SYSCC = &syscc.;
data test;
x = 1;
run;
%put SYSERR = &syserr.;
%put SYSCC = &syscc.;
Log:
68 69 %put SYSERR = &syserr.; SYSERR = 0 70 %put SYSCC = &syscc.; SYSCC = 0 71 72 data test; 73 x = 1 74 run; ___ 22 ERROR 22-322: Syntaxfehler, erwartet wird eines der folgenden: !, !!, &, *, **, +, -, /, <, <=, <>, =, >, ><, >=, AND, EQ, GE, GT, IN, LE, LT, MAX, MIN, NE, NG, NL, NOTIN, OR, ^=, |, ||, ~=. 75 76 ;run; NOTE: The SAS System stopped processing this step because of errors. WARNING: The data set WORK.TEST may be incomplete. When this step was stopped there were 0 observations and 2 variables. WARNING: Datei WORK.TEST wurde nicht ersetzt, da da dieser Schritt angehalten wurde. NOTE: Verwendet wurde: DATA statement - (Gesamtverarbeitungszeit): real time 0.00 seconds user cpu time 0.00 seconds system cpu time 0.00 seconds memory 629.90k OS Memory 20388.00k Timestamp 11.08.2025 09:43:47 vorm. Step Count 47 Switch Count 0 Page Faults 0 Page Reclaims 64 Page Swaps 0 Voluntary Context Switches 0 Involuntary Context Switches 0 Block Input Operations 0 Block Output Operations 16 76 ! /* end step */ 77 78 %put SYSERR = &syserr.; SYSERR = 1012 79 %put SYSCC = &syscc.; SYSCC = 1012 80 81 data test; 82 x = 1; 83 run; NOTE: The data set WORK.TEST has 1 observations and 1 variables. NOTE: Verwendet wurde: DATA statement - (Gesamtverarbeitungszeit): real time 0.00 seconds user cpu time 0.00 seconds system cpu time 0.00 seconds memory 660.03k OS Memory 20388.00k Timestamp 11.08.2025 09:43:47 vorm. Step Count 48 Switch Count 2 Page Faults 0 Page Reclaims 116 Page Swaps 0 Voluntary Context Switches 10 Involuntary Context Switches 0 Block Input Operations 0 Block Output Operations 264 84 85 %put SYSERR = &syserr.; SYSERR = 0 86 %put SYSCC = &syscc.; SYSCC = 1012
SYSERR is reset by a succeeding step, while SYSCC will keep its maximum value.
You could save the LOG of sas programm into a file ,and check if there are any row started with ERROR or WARNING, if there were ,put an ERROR message into a macro variable.
Here is an example:
filename x temp; proc printto log=x new; run; data x; set sashelp.class; array x{*} _numeric_; y=x{10}; run; proc printto; run; %let rc=the code is good ; data _null_; infile x; input; if _infile_ in: ('ERROR' 'WARN') then call symputx('rc','the code is failed'); run; %put &=rc ;
thanks ksharp
one more question please.
is it possible to scan all log files of today in a folder (files name are like ...._20250811.log) and check each file like ?
many thanks
There are so many ways you can do this, more or less complicated and 100% accurate, including coding in SAS, using scripting languages.
There are also a lot of third party tools that manages (and parses) log files.
To advise it would be helpful if you describe a little more about your operating environment nt and the IT organization.
Sure. Of course.
You can scan all these LOG file and output the row started with ERROR or WARN.
And attach the log file name together.
%let path= c:\temp\log ; * the path stored LOG files;
filename x "&path";
data log_file;
did=dopen("x");
do i=1 to dnum(did);
file=dread(did,i);
if lowcase(scan(file,-1,'.'))='log' then do;
log_file=cats("&path",'\',file);
output;
end;
end;
keep log_file;
run;
data want;
set log_file;
length fname filename $ 200;
infile dummy filevar=log_file filename=fname end=last length=len;
filename=fname;
do while(not last);
input log $varying200. len;
if left(log) in: ('ERROR' 'WARN') then output;
end;
run;
If you are OK with just email notifications from a scheduler running your programs then no addtional coding is required and you only need to setup the notifications similar to this:
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.