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

Hi,

 

I am having an issue trying to write a %do macro code that will break when meeting a certain condition. Here is the part of the code I am trying to fix:

 

			if arrival >= min_exit then do;
				wait_time = 0;

				%let success = 0;
				%let e = 1;
				%let nplus1 = %eval(&nbr_svr + 1);
				%do %until (&e = &nplus1);
					if min_exit = exit&e then do;
						exit&e = arrival + time_service_exp;
						%let e = &nplus1;
					end;
					else do;
						%let e = %eval(&e + 1);
					end;
				%end;
			end;
			output;

The end goal is to have the following table (in blue is the data I have before starting the loops

 

arrival + time_service_expexit1exit2exit3min_exit
11000
21- this number is changing because it is the first exit variable equals to min_exit00
3123- this number is changing because it is the first exit variable equals to min_exit0
44231
54532
64563

 

When running, log is showing an infinite loop is being create as it looks like it is still going through the loop for e = n + 1, and then it never ends because it is never true.

 

Thank you in advance for your help!

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

You could try %GOTO statement.

 

%macro x;
%do i=1 %to 10;
  %put &=i.;
  %if &i.=8 %then %goto exit;
%end;

%exit:
%put DONE.;
%mend;

%x

View solution in original post

5 REPLIES 5
Kurt_Bremser
Super User

Since data step statements are not part of macro code, your macro loop is this:

%do %until (&e = &nplus1);
  %let e = &nplus1;
  %let e = %eval(&e + 1);
%end;

and so the exit condition is never met, as &e will always be &nplus + 1.

 

Do not mix macro and Base SAS code like this.

Shawn08
Obsidian | Level 7

Thank you for your insight.

 

I tried using only "do" instead of "%do" (do e = 1 to &nbr_svr;), but it wasn't letting me use the "e" as part of the name of a variable (for example, exit&e).

 

Would you have any advice on how I could approach this problem in order to find a solution?

SASKiwi
PROC Star

Use an ARRAY statement to specify your set of EXIT variables, then use a DO statement to set through them like so:

array exits (*) exit1 - exit10;
do i=1 to dim(exits);
  exits(i) = <logic>;
end;
Patrick
Opal | Level 21

To get answers that contain working and tested code you normally need to provide sample HAVE data, show the desired WANT result and explain the logic how to get from have to want.

Ksharp
Super User

You could try %GOTO statement.

 

%macro x;
%do i=1 %to 10;
  %put &=i.;
  %if &i.=8 %then %goto exit;
%end;

%exit:
%put DONE.;
%mend;

%x

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 5 replies
  • 1951 views
  • 1 like
  • 5 in conversation