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

All, 
    I would like to understand if there is a way to stop a SAS program from executing the steps remaining after it encounters a statement. It is important in my case, that the steps executed before that statement is encountered need to be carried out before execution stops. Here is an example: 

 

%Let t = 1; 

Data Test;
	Set SasHelp.CARS; 
Run; 

%If &t = 1 %Then 
	%Do; 
		Data MyCar; 
			Set Test;
			If Make = 'Mazda'; 
		Run; 
		Quit; 
		
		Data YourCar; 
			Set Test;	
			If Make = 'Audi'; 
		Run; 
	%End; 

   In the example I am showing here, Ideally the dataset MyCar should get created and the program should stop execution. However, the data set YourCar also gets created. I have tried Abort, Stop statements as well with no effect. 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Here's a tool you can play with, and probably get it to work.  Consider this statement:

 

run cancel;

 

The RUN statement contains an option that basically says skip running this step.  The log is not 100% clean ... it still echoes the statements from that step, with a note saying that the step was cancelled.

 

If that result is acceptable, you should be able to utilize this feature by changing all your RUN statements to read:

 

run &can;

 

Then (again, a crude attempt just to get the idea):

 

%let can = cancel;

data test;
set sashelp.cars;
run;

%if &t=1 %then %do;

   data MyCar;
      set test;
      if Make = "Mazda";
   run &can;
   %let can = cancel;
   data YourCar;
      set test;
      if Make = "Audi";
   run &cancel;
   %let can =;

%end;

It's a little awkward, but can be done in open code.  You can also get fancier if you would like by defining a macro.  For example, change all your RUn statements to read %RUNN so you can add %PUT statements to the %RUNN definition.  But that's jumping ahead.  For now, see if this makes sense as an approach.

View solution in original post

8 REPLIES 8
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

your code creates 11 rows in the table mycar and 19 rows in yourcar.

Do you want the process to only find 1 Mazda or all of them?

if the process has already found an Audi before creating the table mycar with a Mazda do we delete the table yourcar with the 19 Audi's?

 

UdayGuntupalli
Quartz | Level 8

@VDD , 
        The dataset is more of an example. I am not particularly interested in the # of rows being returned. I am more interested in being able to control the flow of logic. The instance, a quit statement is encountered, the program should have successfully executed before the statement and nothing after this statement should execute. 

Reeza
Super User

@UdayGuntupalli wrote:

@VDD , 
        The dataset is more of an example. I am not particularly interested in the # of rows being returned. I am more interested in being able to control the flow of logic. The instance, a quit statement is encountered, the program should have successfully executed before the statement and nothing after this statement should execute. 


That's not how QUIT works. QUIT stops specific procs that allow you to submit multiple statements until the proc is 'quit'. Examples are PROC SQL, DATASETS or REG.

 

I think there are functions you can use to do that though but I'm still unclear as to what you would expect as output here.

 

 

Astounding
PROC Star

First step is to define what you mean by having the program quit.  Is it OK to have SAS shut down entirely?  If you're running a batch job, that would be easy and convenient.  But if you're in the middle of an EG session, do you want the session to continue?

 

If it's OK to just shut SAS down, you can do that pretty easily with this statement:

 

endsas;

 

There's also this statement:

 

%abort;

 

So let's talk about what the goal really is first.

UdayGuntupalli
Quartz | Level 8

@Astounding & @Reeza , 
    I would like the SAS session to continue running.Essentially, the objective is to be able to mimic this functionality that is achieved in Macros (http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a000209058.htm) using open code . I would like the ability to capture the results until "quit" or "break" statement is encountered and then stop the execution of the SAS program not the session without generating an error .

Reeza
Super User
You can use macro %IF/%THEN in open code if you're on SAS 9.4M5+
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

the sample data is going to give you the rows into yourcar table because you are not going to find a Mazda before an Audi in the data.

here is a version of what you asked (by using the STOP;) for but not what you really want.

%Let t = 1; 

Data Test;
	Set SasHelp.CARS; 
Run; 
data _null_;
retain carflag 0;
%If &t = 1 %Then 
	%Do; 
		Data MyCar yourcar; 
			Set Test;
			If Make = 'Mazda' then 
				do;
					carflag = 1;
					output mycar;
					*stop;
				end;
			if carflag = 1 then stop;
			else If Make = 'Audi' and carflag ne 1 then output yourcar;
		Run;
		
	%End; 
run;
Astounding
PROC Star

Here's a tool you can play with, and probably get it to work.  Consider this statement:

 

run cancel;

 

The RUN statement contains an option that basically says skip running this step.  The log is not 100% clean ... it still echoes the statements from that step, with a note saying that the step was cancelled.

 

If that result is acceptable, you should be able to utilize this feature by changing all your RUN statements to read:

 

run &can;

 

Then (again, a crude attempt just to get the idea):

 

%let can = cancel;

data test;
set sashelp.cars;
run;

%if &t=1 %then %do;

   data MyCar;
      set test;
      if Make = "Mazda";
   run &can;
   %let can = cancel;
   data YourCar;
      set test;
      if Make = "Audi";
   run &cancel;
   %let can =;

%end;

It's a little awkward, but can be done in open code.  You can also get fancier if you would like by defining a macro.  For example, change all your RUn statements to read %RUNN so you can add %PUT statements to the %RUNN definition.  But that's jumping ahead.  For now, see if this makes sense as an approach.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 8 replies
  • 29178 views
  • 2 likes
  • 4 in conversation