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);
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.
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;
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: