18.4 - Conditionally Executing Do Loops

Printer-friendly versionPrinter-friendly version

As you now know, the iterative DO loop requires that you specify the number of iterations for the DO loop. However, there are times when you want to execute a DO loop until a condition is reached or while a condition exists, but you don't know how many iterations are needed. That's when the DO UNTIL loop and the DO WHILE loop can help save the day!

In this section, we'll first learn about the DO UNTIL and DO WHILE loops. Then, we'll look at another form of the iterative DO loop that combines features of both conditional and unconditional DO loops.

The DO UNTIL Loop

When you use a DO UNTIL loop, SAS executes the DO loop until the expression you've specified is true. Here's the general form of a DO UNTIL loop:

   DO UNTIL (expression);
      action statements;
   END;

where expression is any valid SAS expression enclosed in parentheses. The key thing to remember is that the expression is not evaluated until the bottom of the loop. Therefore, a DO UNTIL loop always executes at least once. As soon as the expression is determined to be true, the DO loop does not execute again.

Example 18.7. Suppose you want to know how many years it would take to accumulate $50,000 if you deposit $1200 each year into an account that earns 5% interest. The following program uses a DO UNTIL loop to perform the calculation for us:

Recall that the expression in the DO UNTIL statement is not evaluated until the bottom of the loop. Therefore, the DO UNTIL loop executes at least once. On the first iteration, the value variable is increased by 1200, or in this case, set to 1200. Then, the value variable is updated by calculating 1200 + 1200*0.05 to get 1260. Then, the year variable is increased by 1, or in this case, set to 1. The first observation, for which year = 1 and value = 1260, is then written to the output data set called investment. Having reached the bottom of the DO UNTIL loop, the expression (value >= 50000) is evaluated to determine if it is true. Since value is just 1260, the expression is not true, and so the DO UNTIL loop is executed once again. The process continues as described until SAS determines that value is at least 50000 and therefore stops executing the DO UNTIL loop.

Launch and run the SAS program, and review the output from the PRINT procedure to convince yourself that it would take 23 years to accumulate at least $50,000.

The DO WHILE Loop

When you use a DO WHILE loop, SAS executes the DO loop while the expression you've specified is true. Here's the general form of a DO WHILE loop:

   DO WHILE (expression);
      action statements;
   END;

where expression is any valid SAS expression enclosed in parentheses. An important difference between the DO UNTIL and DO WHILE statements is that the DO WHILE expression is evaluated at the top of the DO loop. If the expression is false the first time it is evaluated, then the DO loop doesn't even execute once.

Example 18.8. The following program attempts to use a DO WHILE loop to accomplish the same goal as the program above, namely to determine how many years it would take to accumulate $50,000 if you deposit $1200 each year into an account that earns 5% interest:

Launch and run the SAS program, and review the output from the PRINT procedure to convince yourself that ... OOPS! There is no output! The program fails, because in a DO WHILE loop, the expression, in this case (value >= 50000), is evaluated at the top of the loop. Since value is set to missing before the first iteration of the DATA step, SAS can never enter the DO WHILE loop. Therefore, the code proves to be ineffective. Review the log to convince yourself that the investtwo data set contains no observations, because the DO WHILE loop was unable to execute.

Example 18.9. Now, the following program correctly uses a DO WHILE loop to determine how many years it would take to accumulate $50,000 if you deposit $1200 each year into an account that earns 5% interest:

Note that there are just three differences between this program and that of the successful program in Example 18.7 that uses the DO UNTIL loop: i) The value variable is initialized to 0; ii) UNTIL has been changed to WHILE; and iii) the expression to be checked is now (value < 50000). Because value is set to 0 and is therefore less than 50000 at the outset, SAS can now enter the DO WHILE loop to perform our desired calculations.

The calculations proceed as before. First, the value variable is updated to by calculating 0 + 1200, to get 1200. Then, the value variable is updated by calculating 1200 + 1200*0.05 to get 1260. Then, the year variable is increased by 1, or in this case, set to 1. The first observation, for which year = 1 and value = 1260, is then written to the output data set called investthree. SAS then returns to the top of the DO WHILE loop, to determine if the expression (value < 50000) is true. Since value is just 1260, the expression is true, and so the DO WHILE loop executes once again. The process continues as described until SAS determines that value is as least 50000 and therefore stops executing the DO WHILE loop.

Launch and run the SAS program, and review the output from the PRINT procedure to convince yourself that this program also determines that it would take 23 years to accumulate at least $50,000.

Using Conditional Clauses in an Iterative DO Loop

You have now seen how the DO WHILE and DO UNTIL loops enable you to execute statements repeatedly, but conditionally so. You have also seen how the iterative DO loop enables you to execute statements a set number of times unconditionally. Now, we'll put the two together to create a form of the iterative DO loop that executes DO loops conditionally as well as unconditionally.

Example 18.10. Suppose again that you want to know how many years it would take to accumulate $50,000 if you deposit $1200 each year into an account that earns 5% interest. But this time, suppose you also want to limit the number of years that you invest to 15 years. The following program uses a conditional iterative DO loop to accumulate our investment until we reach 15 years or until the value of our investment exceeds 50000, whichever comes first:

Note that there are just two differences between this program and that of the program in Example 18.7 that uses the DO UNTIL loop: i) The iteration i = 1 to 15 has been inserted into the DO UNTIL statement; and ii) because the index variable i is created for the DO loop, it is dropped before writing the contents from the program data vector to the output data set investfour.

Launch and run the SAS program, and review the output from the PRINT procedure to convince yourself that, in this case, the 15 years comes first. That is, the portion of the DO statement that tells SAS to stop executing the DO loop is the iterative i = 1 to 15 part.

Example 18.11. Suppose this time that you want to know how many years it would take to accumulate $50,000 if you deposit $3600 each year into an account that earns 5% interest. Suppose again that you want to limit the number of years that you invest to 15 years. The following program uses a conditional iterative DO loop to accumulate our investment until we reach 15 years or until the value of our investment exceeds 50000, whichever comes first:

There is just one difference between this program and that of the previous program. The amount value is increased has been changed from 1200 to 3600. Launch and run the SAS program, and review the output from the PRINT procedure to convince yourself that, this time, the $50,000 comes first. That is, the portion of the DO statement that tells SAS to stop executing the DO loop is the conditional (value >= 50000) part.

The two examples of using conditional clauses with an iterative DO loop that we looked at involved using a DO UNTIL loop. We alternatively could have used a DO WHILE loop. The main thing to keep in mind is that, as before, the UNTIL expression is evaluated at the bottom of the DO loop, so the DO loop always executes at least once. The WHILE expression is evaluated before the execution of the DO loop. So, if the condition is not true, the DO loop never executes.