BookmarkSubscribeRSS Feed
quickbluefish
Quartz | Level 8
@Quentin - thanks for the tip and the edit!
atesera
Calcite | Level 5

Thanks for your response. will this code (condition abort) stop the current one or all the rest of the programs next in line. Because what I experienced with manually stopping the batch is it stops the current SAS program running but I have several SAS programs within the same batch so when current one is stopped the second one starts running. I want to stop the whole batch or terminate the system so none of the SAS programs after that even starts. 

SASKiwi
PROC Star

If you restructured your programs so that you just scheduled / ran one controlling program that used the %INCLUDE statement to run the others then this would fix your problem.

quickbluefish
Quartz | Level 8

...can confirm that this method proposed by SASkiwi works -- here's an example.  If, for instance, there's an error in PROG2, the master program (the one shown below) will terminate and no remaining programs will be run:

OPTIONS ERRORABEND;

%put running prog1 -- ERR: &syscc ;
%include "/some/path/prog1.sas";

%put running prog2 -- ERR: &syscc ;
%include "/some/path/prog2.sas";

%put running prog3 -- ERR: &syscc ;
%include "/some/path/prog3.sas";

%put running prog4 -- ERR: &syscc ;
%include "/some/path/prog4.sas";

%put DONE -- ERR: &syscc;
quickbluefish
Quartz | Level 8
this will stop the whole chain, because each subsequent program after the failure will look for the presence of the 'continue' dataset and, if not found, will stop immediately. However, I think the post below by SASKiwi is better - I think once SAS goes into an error state (&SYSCC>=7), it will not run any subsequent %INCLUDEd programs - though I've never tested this.
SASKiwi
PROC Star

@quickbluefish - What I typically ensure with %INCLUDE chained jobs is that the SAS OPTION SYNTAXCHECK is set. This is set by default for batch jobs anyway. With this option an error still results in ALL code still running, including the %INCLUDE programs, but no data is processed as OBS = 0 has been set. This means failed jobs finish quickly but still contain complete logs which in my view is ideal for troubleshooting.

Patrick
Opal | Level 21

@SASKiwi Not sure how SAS behaves in current versions but at least in the past even with SAS in syntax check mode some code still executed (like full pass-through SQL) which can lead to issues.

SASKiwi
PROC Star

@Patrick - I haven't seen that behaviour in the versions we use, the oldest being 9.4M2. We in fact use SQL Passthru a lot, and that is most commonly where our programs break. Following an SQL Passthru error we typically see an SQL connection CLI error, SYNTAXCHECK mode switches on and the rest of the program finishes quickly as no further data is processed.

atesera
Calcite | Level 5

@SASKiwi thanks for your response. Could you please include an example of your solution for %INCLUDE. Where should I put that %INCLUDE statement? in the batch or in the SAS programs under the batch

SASKiwi
PROC Star

@atesera  - Up to you where you put the %INCLUDE statements. My preference is to have them in a controlling SAS program - see @quickbluefish 's example earlier in this thread.

atesera
Calcite | Level 5

@SASKiwi @quickbluefish I think it is working now. I believe this option looks for system errors not errors created within the SAS query. The sample query that I was testing had a statement "if Product = '' then put 'ERROR: ' Plan;" and it didnt work for that ERROR but then I tested against a system or syntax error within the query and then it worked. I have to test a bit more but I guess I am confident now that was the case. Thanks for your help

quickbluefish
Quartz | Level 8

OK, right, that isn't going to cause a system error.  If you wanted to do that, you could do something like:


if product=' ' then do;
    put 'ERROR: ' plan=;
    abort abend;
end;
atesera
Calcite | Level 5

ok thanks for that. Yeah, I would like the program to stop even for those errors because sometimes I create those man-made errors to stop the program going further as it will mess up the rest of the datasets. 

quickbluefish
Quartz | Level 8
actually, the syscc thing doesn't seem to work. You might try the `abort abend` method in the edited version above.
Tom
Super User Tom
Super User

@atesera wrote:

ok thanks for that. Yeah, I would like the program to stop even for those errors because sometimes I create those man-made errors to stop the program going further as it will mess up the rest of the datasets. 


You might want to use something like this macro that Tom Hoffman created almost 30 years ago.

(You can replace the calls to his %OPTVAL() macros with calls to %SYSFUNC(GETOPTION()) instead.)

%macro bailout
/*----------------------------------------------------------------------
Stop processing a batch job when specified condition is true AND option
ERRORABEND is turned on. Pop up window in interactive environment.
----------------------------------------------------------------------*/
(cond         /* SAS statement which evaluates to 0 or 1 */
,code=13      /* Abend code */
,msg1=        /* Message 1 */
,msg2=        /* Message 2 */
,msg3=        /* Message 3 */
,msg4=        /* Message 4 */
,msg5=        /* Message 5 */
,env=&sysmenv /* used to fix version 8 bug */
);

/*----------------------------------------------------------------------
This code was developed by HOFFMAN CONSULTING as part of a FREEWARE
macro tool set. 
-----------------------------------------------------------------------
Usage:

%macro test;

%local macro parmerr;
%let macro = TEST;
%parmv(INTERVAL,_req=1,_words=1)
%parmv(IVAR,_req=1)
%if (%length(&visit) > 7) %then
 %parmv(IVAR,_msg=SAS name containing 7 or less characters)
;
%parmv(LZERO,_req=1)
%parmv(UZERO,_req=1)
%parmv(HIGH,_req=1,_val=0 1)
%parmv(DAY,_req=1)
%parmv(PRINT,_req=1,_val=0 1)
%if (&parmerr) %then %goto quit;

... SAS program ...

%quit:

%bailout(&parmerr)

%mend test;
------------------------------------------------------------------------
Notes:

No action is taken when COND=0.

Messages are always written to the log.

When in DMS (but not on the command line), a window with messages will
be displayed.  Message line length maximum is 65 characters.

When ERRORABEND is turned on, the job is aborted.

Note that the BAILOUT macro is typically invoked once at the end of a
report macro. It is not invoked by utility macros that are intended for
use by a report macro. The macro tool programmer must not localize
PARMERR so that it is passed to the calling macro.
-----------------------------------------------------------------------
History:

09OCT96  TRHoffman  Creation
22FEB99  TRHoffman  Add message window for interactive environment,
                    as suggested by Paulette Staum.
11APR00 TRHoffman   Suppressd macro window when invoked from command line.
02JUN00 TRHoffman   Always write messages to log.
12OCT01 TRHoffman   Added ENV parameter to avoid version 8 bug when
                    called indirectly from command line via gsub.
                    (tracking number us5532399).
----------------------------------------------------------------------*/
%local j;

%*----------------------------------------------------------------------
Do nothing when calling macro contains no errors.
-----------------------------------------------------------------------;
%if (%eval(&cond)=0) %then %goto quit;

%*----------------------------------------------------------------------
When not in DMS (either SAS/CONNECT or batch), write messages to log.
When no messages specify, create a default one.
Abort if ERRORABEND is on.
-----------------------------------------------------------------------;
%if (%optval(dms) = NODMS) %then %do;
  %if ^%length(&msg1&msg2) %then
   %let msg1=Submitted code contains errors. See the LOG for details.;
  run;
  data _null_;
    put
     / "&msg1 "
     / "&msg2 "
     / "&msg3 "
     / "&msg4 "
     / "&msg5 "
     / ' '
    ;
  %if (%optval(ERRORABEND) = ERRORABEND) %then %do;
    abort return &code;
  %end;
  run;
%end;

%*----------------------------------------------------------------------
Always write messages to log.
-----------------------------------------------------------------------;
%else %do;
  %do j = 1 %to 5;
    %if %length(&&msg&j) %then %put %quote(&&msg&j);
  %end;

%*----------------------------------------------------------------------
In DMS mode, pop up a window, except when on the command line.
When no messages specified, create a default one.
-----------------------------------------------------------------------;
  %if (&env = S) %then %do;
    %if ^%length(&msg1&msg2) %then
     %let msg1=Submitted code contains errors. See the LOG for details.;
    %window ERRORS columns=65 rows=12 icolumn=5 irow=10
      #1 "&msg1 "
      #2 "&msg2 "
      #3 "&msg3 "
      #4 "&msg4 "
      #5 "&msg5 "
      #7 "Press ENTER to close this window."
    ;
    %display errors bell delete;
  %end;

%*----------------------------------------------------------------------
Switch to log window if on comand line.
-----------------------------------------------------------------------;
  %else %if (&sysmenv = D) %then %do;
    log
  %end;
%end; %* DMS;

%quit:

%mend bailout;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 39 replies
  • 1057 views
  • 3 likes
  • 7 in conversation