BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
aola
Fluorite | Level 6

I have an error handling. The macro %errmail from a documentation of sas with option nonsyntaxcheck.

 

 

filename myfile email
to=&e_mail.
subject= "Error: &number."
type="text/plain";
%macro errmail;
%if &syserr ne 0 %then %do;
options obs= max replace nosyntaxcheck;
data _null_;
file myfile;
put;
put 'ERROR';
put "&syserrortext";
put 'check a log'
run;
%abort cancel;
%end;
%mend errmail;

 

when I have the error in the proc export: (&number. is the table in the work)

 

proc export data=&number.
outfile="/usr/local/backup/&number./&number._%SYSFUNC(TODAY(),DATE9.).txt"
replace
dbms=dlm;
delimiter=';';
run;
%errmail;
ERROR: Physical file does not exist, /usr/local/backup/2116/2116_13MAY2016.txt.
NOTE: The SAS System stopped processing this step because of errors.
NOTE: There were 1 observations read from the data set WORK.2116.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds
 

But for proc import and proc export &syserr is 0 despite errors, so macro is usless here for me.

I need an e-mail about that and I want that SAS stops processing because of macro.

Can I fix it?

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Please note that, according to the documentation, SYSERR "[c]ontains a return code status set by some SAS procedures and the DATA step." Apparently, this does not include all SAS procedures.

 

It is not surprising that SYSERR does not work well with PROC EXPORT, because this procedure is very special: As you can see in the log of a PROC EXPORT step, this procedure in fact executes a data step. This data step produces the error message "Physical file does not exist ..." and it sets SYSERR to a value >0 (on my computer the return code is 1012). However, immediately after this the actual PROC step continues and it writes messages of the following form to the log:

0 records created in ....txt from ...

NOTE: "....txt" file was successfully created.
NOTE: PROCEDURE EXPORT used (Total process time):
...

The note "... successfully created" is questionable, but apparently SAS regards the PROC step as successful, which implies that SYSERR is reset to 0.

 

So, you should use other automatic macro variables, as also Ksharp has suggested. SYSERRORTEXT is a good candidate, because it is not reset after a successful step. In your example it will still contain the error message "Physical file does not exist ...". Another one is SYSCC. In my test it still contains the error code 1012 from the unsuccessful data step (executed by PROC EXPORT).

View solution in original post

12 REPLIES 12
Reeza
Super User

Check what the value of the error variable is right after the proc. If it's being calculated properly, then change your macro to take a paramter

 

proc export data=&number.
outfile="/usr/local/backup/&number./&number._%SYSFUNC(TODAY(),DATE9.).txt"
replace
dbms=dlm;
delimiter=';';
run;

%let error_var = &syserr;

%put &error_var;
Ksharp
Super User
Maybe you could try another macro variable ?

%put &syserrortext ;
aola
Fluorite | Level 6

Sholud I change &syserr ne 0? for syserrortext? But if &syserr=0 for proc export why SAS stops although I have the nosyntaxcheck option?

FreelanceReinh
Jade | Level 19

Please note that, according to the documentation, SYSERR "[c]ontains a return code status set by some SAS procedures and the DATA step." Apparently, this does not include all SAS procedures.

 

It is not surprising that SYSERR does not work well with PROC EXPORT, because this procedure is very special: As you can see in the log of a PROC EXPORT step, this procedure in fact executes a data step. This data step produces the error message "Physical file does not exist ..." and it sets SYSERR to a value >0 (on my computer the return code is 1012). However, immediately after this the actual PROC step continues and it writes messages of the following form to the log:

0 records created in ....txt from ...

NOTE: "....txt" file was successfully created.
NOTE: PROCEDURE EXPORT used (Total process time):
...

The note "... successfully created" is questionable, but apparently SAS regards the PROC step as successful, which implies that SYSERR is reset to 0.

 

So, you should use other automatic macro variables, as also Ksharp has suggested. SYSERRORTEXT is a good candidate, because it is not reset after a successful step. In your example it will still contain the error message "Physical file does not exist ...". Another one is SYSCC. In my test it still contains the error code 1012 from the unsuccessful data step (executed by PROC EXPORT).

aola
Fluorite | Level 6

So I understand I can replace syserr to syscc in the macro %errmail.
What really difference between syscc and syserrortext otherwise the syserrortext is the text of the last error. Is it important which one I use? 
I just wondering because most error handlings what I found based on &syserr. Why if e.g. proc export and proc import have some issues setting &SYSERR properly?

FreelanceReinh
Jade | Level 19

The value of SYSCC reflects the status of the current SAS session. For more details, please see the documentation I linked to in my previous post, in particular for the impact of the ERRORCHECK= system option on SYSCC.

 

Personally, I prefer error handling techniques which strive to avoid error or warning messages issued by SAS as far as possible and instead perform checks before an error occurs. Simple example: Do not divide y by z and then check if some message about "division by zero" has occurred. Instead, check first if z=0 or either operand is missing and if this is the case, take different action.

 

In your example, the error message "Physical file does not exist ..." could be avoided in most cases by checking the existence of that file (or directory) using the FILEEXIST function. If the file or directory does not exist, you can do whatever you deem appropriate in this situation: definitely skip the PROC EXPORT step, maybe exit the program or write a user-defined message to the log, send an email, ...

SASKiwi
PROC Star

My opinion is that this is a bug and SYSERR should be set as it would be if this were a user-written DATA step.

 

I use SAS batch processing a lot and I rely on the job return code being non-zero if there is an error somewhere in the job. This also causes the batch job to go into syntax check mode and avoid creating any suspect data. With this PROC EXPORT or IMPORT bug (same problem with IMPORT) you cannot rely on the job return code to tell if it is successful or not. You have to go searching through the log to see if any IMPORT/EXPORT step failed. OK you could add your own error checking but you shouldn't have to in this case.  

aola
Fluorite | Level 6

If I use %syserrortext I suppose

 %abort cancel; 

overrides me an error that interests me. 

%if %length(&syserrortext) ne 0 %then %do;  

can I get the penultimate &syserrortext?

Ksharp
Super User
You can use %return; instead of %abort;
aola
Fluorite | Level 6

but now I got a few e-mails with one error because sas dont stop and goes further:(

 

%macro errmail;                                                                                                                         
 %if %length(&syserrortext) ne 0 %then %do;  
options obs=max noreplace nosyntaxcheck; 

  data _null_;        
   file myfile;                                                                                                                                                                                               
   put;                                                                                                                                 
   put 'ERROR';                                                                                                                    
   put "&syserrortext";                                                                                                                 
   put;
	put "Log: \logs" ;
  run;       
 %end;   
 %return cancel; 
%mend errmail;
Ksharp
Super User
%return cancel; 

-->

%return; 

 ?

 

I noticed you should include %return into %do; %end;

 

%let flag=1;

%macro xxx;
%if &flag=1 %then %do;
 
 %return ;
%end;
%else %do;
 %put NOTE: flag=&flag;
%end;
%mend;


%xxx

 

OR, don't use %return; or %abort; , just data step code ?

 

%macro errmail;                                                                                                                         
 %if %length(&syserrortext) ne 0 %then %do;
  
options obs=max noreplace nosyntaxcheck; 
  data _null_;        
   file myfile;                                                                                                                                                                                               
   put;                                                                                                                                 
   put 'ERROR';                                                                                                                    
   put "&syserrortext";                                                                                                                 
   put;
	put "Log: \logs" ;
  run;       
 %end;   

%mend errmail;
Ksharp
Super User
Or maybe you run some dummy code to reset &syserrortext . Like :



%macro errmail;                                                                                                                         
 %if %length(&syserrortext) ne 0 %then %do;
  
options obs=max noreplace nosyntaxcheck; 
  data _null_;        
   file myfile;                                                                                                                                                                                               
   put;                                                                                                                                 
   put 'ERROR';                                                                                                                    
   put "&syserrortext";                                                                                                                 
   put;
	put "Log: \logs" ;
  run;       
 %end;   

%mend errmail;




data _null_;
 set sashelp.class;
run;
proc print data=sashelp.class;run;

%errmail

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 12 replies
  • 3045 views
  • 0 likes
  • 5 in conversation