DATA Step, Macro, Functions and more

advantage of DO UNTIL

Accepted Solution Solved
Reply
Regular Contributor
Posts: 234
Accepted Solution

advantage of DO UNTIL

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


Accepted Solutions
Solution
‎03-18-2016 09:47 AM
Super User
Super User
Posts: 6,502

Re: advantage of DO UNTIL

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


All Replies
Super User
Posts: 5,260

Re: advantage of DO UNTIL

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
Regular Contributor
Posts: 234

Re: advantage of DO UNTIL

@ LinusH. I believe looping is very important part of any programming language. I really want to master on it.
Solution
‎03-18-2016 09:47 AM
Super User
Super User
Posts: 6,502

Re: advantage of DO UNTIL

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;
Super User
Posts: 10,532

Re: advantage of DO UNTIL

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.

 

Regular Contributor
Posts: 200

Re: advantage of DO UNTIL

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

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 421 views
  • 5 likes
  • 5 in conversation