BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
hmlong25
Obsidian | Level 7

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;

?

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

You can run the two programs and compare the results. You don't need us to work on this.

--
Paige Miller

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26

You can run the two programs and compare the results. You don't need us to work on this.

--
Paige Miller
yabwon
Onyx | Level 15

Exactly! Perfect use case for Maxim 4: "If in doubt, do a test run and look at the results. If puzzled, inquire further." 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Amir
PROC Star

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.

Cynthia_sas
SAS Super FREQ

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:

 

Cynthia_sas_0-1745933057391.png

 

  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:

Cynthia_sas_1-1745934018583.png

  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

Tom
Super User Tom
Super User

@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.

 

hmlong25
Obsidian | Level 7
Best to you! That clarifys my future use of Do Until and While in programming! Yay!

sas-innovate-white.png

Our biggest data and AI event of the year.

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.

 

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
  • 7 replies
  • 612 views
  • 13 likes
  • 6 in conversation
OSZAR »