BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
danxy
Calcite | Level 5

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.

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

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

If the method of operation is windowing environment and interactive line mode, use the CANCEL option to do the following:
  • It only clears the current submitted program.
  • Other subsequent submitted programs are not affected.
  • The error message is written to the SAS log

 

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

  1. Did not create data set T2
  2. Did not create data set T3
  3. The windows SAS session stayed open waiting for me to edit the program.

I got the same results running sas/studio.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

1 REPLY 1
mkeintz
PROC Star

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

If the method of operation is windowing environment and interactive line mode, use the CANCEL option to do the following:
  • It only clears the current submitted program.
  • Other subsequent submitted programs are not affected.
  • The error message is written to the SAS log

 

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

  1. Did not create data set T2
  2. Did not create data set T3
  3. The windows SAS session stayed open waiting for me to edit the program.

I got the same results running sas/studio.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 1 reply
  • 524 views
  • 4 likes
  • 2 in conversation