Desktop productivity for business analysts and programmers

&syserr in proc export

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

&syserr in proc export

[ Edited ]

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?

 

 

 


Accepted Solutions
Solution
‎05-15-2016 10:49 AM
Trusted Advisor
Posts: 1,116

Re: &syserr in proc export

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


All Replies
Super User
Posts: 19,157

Re: &syserr in proc export

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;
Super User
Posts: 9,867

Re: &syserr in proc export

Maybe you could try another macro variable ?

%put &syserrortext ;
Occasional Contributor
Posts: 12

Re: &syserr in proc export

[ Edited ]

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

Solution
‎05-15-2016 10:49 AM
Trusted Advisor
Posts: 1,116

Re: &syserr in proc export

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).

Occasional Contributor
Posts: 12

Re: &syserr in proc export

[ Edited ]

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?

Trusted Advisor
Posts: 1,116

Re: &syserr in proc export

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, ...

Super User
Posts: 3,235

Re: &syserr in proc export

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.  

Occasional Contributor
Posts: 12

Re: &syserr in proc export

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?

Super User
Posts: 9,867

Re: &syserr in proc export

You can use %return; instead of %abort;
Occasional Contributor
Posts: 12

Re: &syserr in proc export

[ Edited ]

but now I got a few e-mails with one error because sas dont stop and goes furtherSmiley Sad

 

%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;
Super User
Posts: 9,867

Re: &syserr in proc export

[ Edited ]
%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;
Super User
Posts: 9,867

Re: &syserr in proc export

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

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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