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

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 5 replies
  • 1366 views
  • 6 likes
  • 5 in conversation