HI
I have the code as below.
I want to stop the %do loop at first iteration because dataset e will have 0 obs. So it will not execute more statements inside the loop 1. (because it's a huge code and take very long)
Then loop 2 should begin normally and execute till end because dataset e will have observations.
Any ways to achieve this ?
I did research and Leave statement can do this in normal do loop but it won't work for macro loop.
Thanks.
%macro checkit;
%DO i= 1 %to 2;
data e ;
x=2 ;
if x=&i;
run;
/* huge code and multiple loops here.....*/
%END;
%mend checkit;
%checkit;
You should be able to use %GOTO. For example:
%macro checkit;
%DO i= 1 %to 2;
data e ;
x=2 ;
if x=&i;
run;
%if &i=1 %then %goto finish;
/* huge code and multiple loops here.....*/
%finish: %END;
%mend checkit;
%checkit;
Hello,
See here :
SAS® 9.4 and SAS® Viya® 3.5 Programming Documentation | SAS 9.4 / Viya 3.5
Macro Language Reference
%GOTO Macro Statement
Example: Providing Exits in a Large Macro
https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/p0jfeh75p72icen1ddd9una5zbmm.htm
To check the n° of observations in your dataset, use this code :
data _NULL_;
if 0 then set sashelp.shoes nobs=count;
call symput('numobs',trim(left(put(count,11.))));
STOP;
run;
%PUT &=numobs;
Good luck,
Koen
You should be able to use %GOTO. For example:
%macro checkit;
%DO i= 1 %to 2;
data e ;
x=2 ;
if x=&i;
run;
%if &i=1 %then %goto finish;
/* huge code and multiple loops here.....*/
%finish: %END;
%mend checkit;
%checkit;
Wrap the code you want to execute conditionally in a %IF <condition> %THEN %DO; %END; block.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.