True or False: These two programs generate the same result.
data work.invest; capital=100000; do until(Capital gt 500000); Year+1; capital+(capital*.10); end; run; data work.invest; capital=100000; do while(Capital le 500000); Year+1; capital+(capital*.10); end; run;
?
You can run the two programs and compare the results. You don't need us to work on this.
You can run the two programs and compare the results. You don't need us to work on this.
Exactly! Perfect use case for Maxim 4: "If in doubt, do a test run and look at the results. If puzzled, inquire further."
Hi,
Part of the title of your post says "confusing that both are true". By "true" do you mean "equal"? Do you think they should be different? If yes, what do you think the difference should be and why? Basically, what is the confusion?
You might already be aware, but in case you are not, the do until condition is only checked to see if the loop should be repeated after the code in the loop is executed. Whereas the do while condition is checked before the code in the loop to see if any looping is required.
If you tried running the code as advised by others and still cannot understand your results, then try using a debugger if you have one.
Thanks & kind regards,
Amir.
Hi:
If you run both programs, as suggested, you'll see that the results of both programs are the same whether you use a WHILE or an UNTIL:
However, look at the condition that was needed to make each DO loop work to produce the same results. The UNTIL code uses (Capital GT 500000) The UNTIL will ALWAYS have the loop executed at least once because the condition is checked at the bottom of the DO loop, after the statements have executed at least one time.
The condition for the WHILE was (Capital LE 500000) and the condition for a WHILE is checked at the top of the loop. So in this example, both loops would run the same number of times to produce the same results.
If you have data and some logic that needs to be executed at least one time if the initial condition is met, then use a DO UNTIL. If you have some logic that you do NOT want to execute if the initial condition is met, then use a DO WHILE. This first example shows that both forms of the DO loop are capable of producing the same results, given that you have coded the condition correctly.
However, to prove that it is possible for the 2 loops to produce different results, what if in the example program, we started the value for Capital at exactly 500000 and what if we changed the logical operator in the condition for the WHILE -- so that instead of testing for LE, we just tested for LT -- then the results WOULD be different:
Now you see that the program with DO UNTIL produced different results than the program with DO WHILE. This is not always the case, as the first program proves. You are in control of the operators used for the DO loop conditions.
The important things to learn about DO UNTIL/DO WHILE are: where does the condition for the loop get tested? Then, for the data and problem you are working with, do you need for the statements to execute at least one time or not? Finally, what logical operator do you need to use to make your loop work with your data? In the first program, we wanted to illustrate how it was possible for both forms of the DO loop to produce the same results. The key to making that happen was the logical operator we used in the code for the question.
Hope this helps explain things a bit more.
Cynthia
@Cynthia_sas wrote:
...
Finally, what logical operator do you need to use to make your loop work with your data? In the first program, we wanted to illustrate how it was possible for both forms of the DO loop to produce the same results. The key to making that happen was the logical operator we used in the code for the question.
...
If you want the two loops to be similar the key is that the logical expression used in the WHILE() and UNTIL() return opposite results.
So to preform the same test as used by the loop DO WHILE(A) you need to have use DO UNTIL(NOT A).
In the example code the conditions (Capital gt 500000) and (Capital le 500000) meet this criteria.
Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.
Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.
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.