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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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;

View solution in original post

4 REPLIES 4
ballardw
Super User

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;
solfay243
Obsidian | Level 7

Many thanks!

 

I wasn't aware of the "LEAVE" statement and hadn't read that anywhere before!

gamotte
Rhodochrosite | Level 12

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;
Tom
Super User Tom
Super User

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..

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 759 views
  • 1 like
  • 4 in conversation