BookmarkSubscribeRSS Feed
sashelp123
Calcite | Level 5

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

 

 

 

 

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

 

 

Tom
Super User Tom
Super User

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;
s_lassen
Meteorite | Level 14

@sashelp123:

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 3477 views
  • 0 likes
  • 4 in conversation