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.
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.
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?
@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.
@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.
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.
@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 .
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;
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.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.