Stop processing SAS program when certain condition is met

Reply
New Contributor
Posts: 4

Stop processing SAS program when certain condition is met

Hello, I am trying to stop processing a SAS program (SAS 9.4) when a certain condition is met. here is my code so far. Basically if the count is higher than 0 I would like the program to stop processing immediately and a message to pop up. If the counts are 0, I would like it to continue processing the following code. Thank you

 

proc sql;

select count(*) into :nb_counts

from new_fee_check;

quit;

 

 

%macro zzz(when);

%if &when ne 0 %then %do;

dm 'post "The ZH_Payments table is done"';

%end;

%mend;

%zzz(&nb_counts);

 

 

 

 

Super User
Super User
Posts: 8,634

Re: Stop processing SAS program when certain condition is met

Posted in reply to rcamphaug

Afraid this is typical wrong way around thinking.  Doing something unecessary, and then mashing some macro code in to cover up.  

In the SASHELP.VTABLE dataset (well view), there is a list of all datasets found in all libraries in the SAS system.  This dataset also contains the observation count for each one.  Hence the need to open and count each dataset is irrelevant.  To get a list of datasets with data you can simply do:

data want;
  set sashelp.vtable (where=(libname="ABC" and nobs=0));
run;

For more precise code, post example test data in the form of a datastep and require output.

 

 

Super User
Super User
Posts: 7,401

Re: Stop processing SAS program when certain condition is met

Posted in reply to rcamphaug

It depends a lot on how you are submitting the program to run. If you are running the program directly from the file using the command line or batch submission process then you could just use the ABORT statement to kill the process.

 

But that will not work very well when submitting a block of code from the program editor in SAS Display Manager or when basically rsubmitting the code to a server process when using SAS EG or SAS/Studio user interface.  In those environments you would need to code your whole process to handle the possibility of skipping steps.  

 

What we used to use when we used Display Manager a lot was to have a macro that could test a condition and then conditionally run a %DISPLAY command.  That way the process would stop and the user could hit the break key to stop the submitted code.  Something like this.

    %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;
PROC Star
Posts: 169

Re: Stop processing SAS program when certain condition is met

Posted in reply to rcamphaug

@rcamphaug:

You could use the %ABORT CANCEL FILE statement to stop processing the statements submitted:

proc sql;
select count(*) into :nb_counts
from new_fee_check;
quit;
 
 
%macro zzz(when);
%if &when ne 0 %then %do;
dm 'post "The ZH_Payments table is done"';
%abort cancel file; 
%end;
%mend;
%zzz(&nb_counts);

It would then stop processing the current program submitted - you will get an error message in the log, though:

 

8    %macro zzz(when);
9    %if &when ne 0 %then %do;
10   dm 'post "The ZH_Payments table is done"';
11   %abort cancel file;
12   %end;
13   %mend;
14   %zzz(3);

ERROR: Execution canceled by an %ABORT CANCEL FILE statement.
NOTE: The SAS System stopped processing due to receiving a CANCEL request.

but you will not have your session killed if you are running SAS EG or Display Manager.

Ask a Question
Discussion stats
  • 3 replies
  • 139 views
  • 0 likes
  • 4 in conversation