BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SAS_inquisitive
Lapis Lazuli | Level 10

What advantage can be made of DO UNTIL over DO WHILE  in programming because of the fact that DO UNTIL executes at least once?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

In general the advantage is not having the repeat the same block of code before the DO loop and inside the DO loop.

So you can convert code like:

< get an item >
do while (not <condition>);
  <do something>
  <get an item>
end;

to 

do until (<condition>);
  <get an item>
  <do something>
end;

View solution in original post

5 REPLIES 5
LinusH
Tourmaline | Level 20
Mainly semantics I guess. And save a line of code or two compared to while, given the situation.
You seen very interested in these matters, any specific problem you wish to solve?
The while and until constructs exists in many programming languages. There must lot of literature to browse that can guide you.
Data never sleeps
SAS_inquisitive
Lapis Lazuli | Level 10
@ LinusH. I believe looping is very important part of any programming language. I really want to master on it.
Tom
Super User Tom
Super User

In general the advantage is not having the repeat the same block of code before the DO loop and inside the DO loop.

So you can convert code like:

< get an item >
do while (not <condition>);
  <do something>
  <get an item>
end;

to 

do until (<condition>);
  <get an item>
  <do something>
end;
ballardw
Super User

One generic "difference" is along the lines of "test at the top" and "test at the bottom". "Do While" means the intial value usually is set before the loop and often executes at least once because of the initial value. "Do until" is more often used to check the condition at the end of one execution fo the loop.

 

Or treat as "do until this condition that is initially true is no longer true" and "Do until this condtion that is initially false is true"

 

Usually with work one can get the same results with either loop construct.

Be very leery of using Do Until or Do While with non-integer numerics and and Equal comparison especially if calculating the loop control as you might not get "equality" do to a variety of reasons with the sneakiest being precision issues.

 

Consider this program:

data _null_;
   x=0;
   do until(x=1);
      x=x+0.01;
   end;
run;

Do not run this if you do not know how to interrupt a program.

 

Ron_MacroMaven
Lapis Lazuli | Level 10

getting straight on where the exit test happens is an important aspect of understanding

the difference between while (at top) and until (at bottom).

 

There are two other important loop constructs:

continue

and

leave

 

both of which can be implemented in macro loops as well.

 

Check my paper for details

 

http://www.sascommunity.org/wiki/Do_which_loop_while_or_until

 

Ron Fehd  loops maven

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 1868 views
  • 6 likes
  • 5 in conversation