DATA Step, Macro, Functions and more

Conditionally terminate SAS with no error?

Accepted Solution Solved
Reply
Contributor
Posts: 53
Accepted Solution

Conditionally terminate SAS with no error?

Hi guys,

I'm running SAS 9.3 on a Windows 2008R2 server and I have a 3rd party scheduler that runs my scheduled SAS jobs. 

I have a few SAS jobs that can periodically have no records in them so I conditionally create a report telling the user that there were no records found.  The issue comes when I need to end SAS after I create this "dummy" report. 

I've tried the "abort return 0" and "%abort return 0" statements but it appears that SAS is still returning a non 0 return code.  Is there any way to stop SAS from processing further without causing an error? 


Accepted Solutions
Solution
‎11-08-2012 11:41 AM
Respected Advisor
Posts: 3,156

Re: Conditionally terminate SAS with no error?

Maybe something like this: you can play with obs=0/1 to see the different results:

%macro test;

data _1;

   set sashelp.class (obs=0);

run;

data _null_;

call symputx('nobs',nobs);

set _1 nobs=nobs;

  stop;

run;

%if &nobs=0 %then %do;

    %put SAS session is aborted error_rc=&syserr;

       

     %goto exit;

  %end;

  %put Normal SAS session continuned;

%exit:

%mend;

%test;

Haikuo

View solution in original post


All Replies
Respected Advisor
Posts: 3,156

Re: Conditionally terminate SAS with no error?

More information is probably needed here. so I guess you use 'abort' to conditionally stop the batch process, and after doing that you are not happy that &syserr or &sqlrc is not zero.

I hate to break this to you, but if you choose to use 'abort', there is no way to trick SAS not to put an error on record somewhere. So my question gets down to this: Is it possible for you to wrap up your whole code in Macro, and whenever your 'abort' condition is met, you just leap out of Macro and stop the whole process without literally using 'abort' statement?

Haikuo

Contributor
Posts: 53

Re: Conditionally terminate SAS with no error?

Yep you're correct.   Actually the code creating either the actual report or the 'dummy' empty report is already wrapped in a macro and it's always the last macro in the job.  So jumping out of the macro would work for me I think (?)  Can you point me to the statements I need to do that?

Solution
‎11-08-2012 11:41 AM
Respected Advisor
Posts: 3,156

Re: Conditionally terminate SAS with no error?

Maybe something like this: you can play with obs=0/1 to see the different results:

%macro test;

data _1;

   set sashelp.class (obs=0);

run;

data _null_;

call symputx('nobs',nobs);

set _1 nobs=nobs;

  stop;

run;

%if &nobs=0 %then %do;

    %put SAS session is aborted error_rc=&syserr;

       

     %goto exit;

  %end;

  %put Normal SAS session continuned;

%exit:

%mend;

%test;

Haikuo

Contributor
Posts: 53

Re: Conditionally terminate SAS with no error?

Thanks very much for your help.  I used a combo of your suggestion and this paper (http://www.sascommunity.org/sugi/SUGI95/Sugi-95-73%20Tilanus.pdf).

Super User
Posts: 10,041

Re: Conditionally terminate SAS with no error?

%RETURN;

wouldn't be able to work ?

Respected Advisor
Posts: 3,799

Re: Conditionally terminate SAS with no error?

Maybe the macro processes a data set and there is some kind of subsetting option, an expression, that is passed to a WHERE statement.  And the macro creates a working copy of the data applying any observation or variable subsets required.  The condition we seek "Are there NONE?" can be checked when that data is created.

%macro main(data=sashelp.class,where=);
  
%local error_rc;  
  
%let error_rc = 0;
   data &sysmacroname._1;
      if _n_ eq
1 and eof then do;
         call symputX('error_rc',1,'local');
         put 'NOTE: Macro ending due to no observations returned from data.';
         stop;
         end;
      set &data end=eof;
      where &where;
      run;
  
%if &error_rc %then %do;
     
%put NOTE: Macro &sysmacroname aborted error_rc=&error_rc;
      %goto exit;
      %end;
  
%put NOTE: Normal SAS session continuned;

%exit:
   proc datasets library=work nolist;
      delete &sysmacroname._:;
      run;
      quit;
  
%put NOTE: Macro &sysmacroname ending.;
   %mend;

%
main(where=age eq 9);
%main(data=sashelp.shoes(obs=0));
Respected Advisor
Posts: 3,156

Re: Conditionally terminate SAS with no error?

Posted in reply to data_null__

Thanks for sharing, DN. Strong and flexible.

Haikuo

Super User
Posts: 11,343

Re: Conditionally terminate SAS with no error?

May want to see if ENDSAS or BYE works.

🔒 This topic is solved and locked.

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

Discussion stats
  • 8 replies
  • 5703 views
  • 0 likes
  • 5 in conversation