Is there a way to make SAS stop upon the first war

2020-01-29 06:02发布

问题:

SAS likes to continue processing well after warnings and errors, so I often need to scroll back through pages in the log to find an issue. Is there a better way? I'd like it to stop as soon as the first error or warning appears so I can fix it and try again.

回答1:

The ERRORS=1 option was previously suggested, but that only stops he ERROR messages from writing to the log. I would suggest another system option ERRORABEND which will stop the program from further processing for most errors. I don't know of an option to terminate processing due to warnings, but I think that you could add a macro like the following to stop processing.

%macro check_for_errors;
   %if &syserr > 0 %then %do;
      endsas;
   %end;
%mend check_for_errors;

data test1;
    <data step code>
run;
%check_for_errors;

You could repeat the macro call after each step of your program, and it should terminate at the point that the error code is anything but 0.



回答2:

I've been using the %runquit macro recently. Works well for both batch jobs and interactive sessions (doesn't close your session, just stops running the code).

Source: http://www.cpc.unc.edu/research/tools/data_analysis/sas_to_stata/sas-macros/runquit.html

To use it you basically type %runquit; at the end of any data step or PROC instead of typing your regular run or quit statement.

Code:

%macro runquit;
  ; run; quit;
  %if &syserr. ne 0 %then %do;
     %abort cancel;
  %end;
%mend runquit;

Datastep usage:

data something; 
 * do some stuff;
%runquit;

PROC usage:

proc sql; 
  * do some stuff;
%runquit;

It's not quite as pretty when reading through code, but it does make debugging a lot easier.



回答3:

One option is to replace run with run &g_cancel throughout, and proc sql; with proc sql &g_noexec;. Initially &g_cancel and &g_noexec are set to nothing so everything runs.

On hitting an error (either %sys_rc, %sql_rc or using referring to business logic) set &g_cancel to cancel and &g_noexec to noexec.

This should stop any subsequent steps from running - obviously the macro variables can be omitted for steps that have to run regardless (for instance a tidy up) or checked before performing steps purely in macro.

Note for Enterprise Guide users: The only warning note is that if you are running multiple code items in the same session you will need to reset the error terms at the start of each code item, lest unrelated errors stop anything working.



回答4:

I frequently do something similar to RWill, but I wrap my entire program in a macro. After each DATA step, PROC SQL, PROC SORT, etc. I check for an error code (&SYSERR or &SQLRC). If it is non-zero, I jump to the end.

More details and code here: https://heuristically.wordpress.com/2012/02/09/return-codes-errors-sas/

I cannot use RWill's endsas because of how our organization's batch system runs independent programs in one SAS session.



回答5:

As complement to Rwill's answer:

If you are using a stored process (STP), it's also nice to not show the log to your users when an error occurs and to remove the "Show SAS log" button.

That can be achieved with this

%macro checkcc;
    options obs=max no$syntaxcheck;
    %if (&syscc gt 4) %then
        %do;
            data _null_;
                file _webout;
                put "<h3>Sorry, your request was not processed successfully.<h3>";
                rc = stpsrvset('program error', 0);
            run;
        %end;
    %let syscc=0;
%mend checkcc;
%checkcc;

source : http://support.sas.com/kb/16/225.html

And here is an enhanced version that I made to still show the error in json format.

%macro checkErrors;
    options obs=max no$syntaxcheck;
    %let old = %sysfunc(stpsrv_header(Content-type, application/json%str(;) charset=utf-8));
    %put &=syscc; %put &=syserr; %put &=sysrc; %put &=syswarningtext; %put &=syserrortext;
    %if (&syscc gt 4) %then %do;
        data _null_;
            file _webout;
            put '{';
            put '  "success":"false"';
            put '  ,"message":"' "&syserrortext" '"';
            put '  ,"syscc":"' "&syscc" '"';
            put '}';
            rc = stpsrvset('program error', 0);
            run;
        %end;
    %let syscc=0;
%mend checkErrors;
%checkErrors;

and HTML version :

%macro checkErrors_HTML;
    options obs=max no$syntaxcheck;
    %if (&syscc gt 4) %then %do;
        data _null_;
            file _webout;
            put '<!doctype html> ';
            put '<html> ';
            put '   <head> ';
            put '       <title>Error</title> ';
            put '   </head> ';
            put '   <body> ';
            put '       <h1>An Error Occured</h1>';
            put '       <p>' "&syserrortext" '</>';
            put '   </body>';
            put '</html>';
            rc = stpsrvset('program error', 0);
            run;
        %end;
    %let syscc=0;
%mend checkErrors_HTML;
%checkErrors_HTML;


标签: sas