BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Nasser_DRMCP
Lapis Lazuli | Level 10

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

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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;

 

 

View solution in original post

8 REPLIES 8
Kurt_Bremser
Super User

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.

 

 

Nasser_DRMCP
Lapis Lazuli | Level 10

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 

Kurt_Bremser
Super User

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. 

Ksharp
Super User

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 ;
Nasser_DRMCP
Lapis Lazuli | Level 10

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

LinusH
Tourmaline | Level 20

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.

Data never sleeps
Ksharp
Super User

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;

 

 

SASKiwi
PROC Star

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:

SASKiwi_0-1754963178525.png

 

 

 

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

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
  • 8 replies
  • 446 views
  • 5 likes
  • 5 in conversation