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

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;
1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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;

View solution in original post

3 REPLIES 3
sbxkoenk
SAS Super FREQ

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

Astounding
PROC Star

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;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1025 views
  • 4 likes
  • 4 in conversation