I already have a long SAS code that will extract transform and analyze the data. I want to build in some checks for the data in the middle of the program. Ideally, if the check fails, the rest of the code should stop executing so user can review, edit and restart the program where the code stopped.
I tried to use %abort to stop the program, but it only stops the macro and the rest of the program still executes.
I tried to use %abort abend and %abort return, it stopped executing the rest of the program but also closed SAS (which I do not want).
The documentation for %abort abend and %abort return says: "further action is based on how your operating environment and your site treat jobs that end abnormally".
Can anyone help determine a method to either update my "environment" so SAS is not closed when the "jobs end abnormally" or an alternative method to achieve my desired results. Please do not propose any solutions that requires the rest of the program to be put in a conditional statement as the program combined with multiple instances of this check will make the program unwieldy for future edits.
Please see code I used to test abort feature below:
data test_data;
j = 1;
do i = 1 to 10;
output;
end;
run;
%macro test_for_duplicates (input_dataset, primary_key);
proc sort data = &input_dataset dupout = duplicate_records nodupkey;
by &primary_key;
run;
proc sql noprint;
select count(*) into: dup_obs_count from duplicate_records;
quit;
%if &dup_obs_count. > 0 %then %do;
%put "error message here";
%abort;
%end;
%mend;
%test_for_duplicates(test_data, j);
%put "rest of program here";
Thanks.
I presume you are running SAS interactively, since you want SAS to stop processing code, but not close. That wouldn't make sense in a batch environment.
Try %abort cancel;
which is documented as
So, in my windows sas session I submitted this code:
%macro test;
proc sql;
create table t as
select * from sashelp.class where sex='X';
quit;
%if &sqlobs=0 %then %abort cancel;
data t2;
set t;
run;
%mend test;
%test;
data t3; set t; run;
Because sashelp.class has no obs with sex='X' it made data set T with 0 observations, making &SQLOBS=0 and therefore executing the %abort cancel statement. The results were
I got the same results running sas/studio.
I presume you are running SAS interactively, since you want SAS to stop processing code, but not close. That wouldn't make sense in a batch environment.
Try %abort cancel;
which is documented as
So, in my windows sas session I submitted this code:
%macro test;
proc sql;
create table t as
select * from sashelp.class where sex='X';
quit;
%if &sqlobs=0 %then %abort cancel;
data t2;
set t;
run;
%mend test;
%test;
data t3; set t; run;
Because sashelp.class has no obs with sex='X' it made data set T with 0 observations, making &SQLOBS=0 and therefore executing the %abort cancel statement. The results were
I got the same results running sas/studio.
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 25. 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.