What advantage can be made of DO UNTIL over DO WHILE in programming because of the fact that DO UNTIL executes at least once?
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;
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;
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.
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
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!
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.