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_exp | exit1 | exit2 | exit3 | min_exit |
| 1 | 1 | 0 | 0 | 0 |
| 2 | 1 | 2 - this number is changing because it is the first exit variable equals to min_exit | 0 | 0 |
| 3 | 1 | 2 | 3- this number is changing because it is the first exit variable equals to min_exit | 0 |
| 4 | 4 | 2 | 3 | 1 |
| 5 | 4 | 5 | 3 | 2 |
| 6 | 4 | 5 | 6 | 3 |
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!
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
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.
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?
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;
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.
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
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.