My subject line gives the basic premise of my issue. Forgive me if this is elementary, but I can't find SAS documentation outlining it.
Basically, can I use a DO i=1 to Z loop with an UNTIL conditional statement? I'm also wondering if I can use multiple different UNTIL conditions as a stopping point (so whether I can and how I can use an OR operator in the UNTIL conditional).
I figured this would be possible, but when I try to process my macro I get the following:
ERROR 68-185: The function UNTIL is unknown, or cannot be accessed.
Possible but likely not easily . IF you want to break out of a look that would normally execute Z times but a condition inside the loop sets a value such that you would like to end the loop the instruction is LEAVE. The dummy code for the loop would look like
do I = 1 to z; <some statements> if x=1 then leave; <other statements to execute if not leaving the loop> end;
Possible but likely not easily . IF you want to break out of a look that would normally execute Z times but a condition inside the loop sets a value such that you would like to end the loop the instruction is LEAVE. The dummy code for the loop would look like
do I = 1 to z; <some statements> if x=1 then leave; <other statements to execute if not leaving the loop> end;
Many thanks!
I wasn't aware of the "LEAVE" statement and hadn't read that anywhere before!
Hello,
Can you show the code you used ? The following code works.
data have;
z=10;
x=5;
do i=1 to z until(x=1);
x+(-1);
output;
end;
run;
You cannot have multiple UNTIL() (or have both an UNTIL() and a WHILE()).
Note you can easily test such things yourself by just generating the code yourself BEFORE trying to create a macro to generate the code.
But the condition in the UNTIL () could be as complex as you want.
do i=1 to z until(x=1 or y<0 or (w1=3 and w2=4) );
If you are comfortable debugging spaghetti code you could use the LEAVE statement to exit the loop from the middle..
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.
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.