Conditionally Executing DO Loops

Overview

The iterative DO statement specifies a fixed 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 do not know how many iterations are needed.
Suppose you want to calculate the number of years required for an investment to reach $50,000. In the DATA step below, using an iterative DO statement is inappropriate because you are trying to determine the number of iterations required for Capital to reach $50,000.
data work.invest; 
   do year=1 to ? ; 
      Capital+2000; 
      capital+capital*.10; 
   end; 
run;
The DO WHILE and DO UNTIL statements enable you to execute DO loops based on whether a condition is true or false.

Using the DO UNTIL Statement

The DO UNTIL statement executes a DO loop until the expression becomes true.
Syntax, DO UNTIL statement:
DO UNTIL(expression);
...more SAS statements...
END;
expression is a valid SAS expression enclosed in parentheses.
The expression is not evaluated until the bottom of the loop. Therefore, a DO UNTIL loop always executes at least once. When the expression is evaluated as true, the DO loop stops.
Assume you want to know how many years it takes to earn $50,000 if you deposit $2,000 each year into an account that earns 10% interest. The DATA step below uses a DO UNTIL statement to perform the calculation until $50,000 is reached. Each iteration of the DO loop represents one year.
data work.invest; 
   do until(Capital>=50000); 
      capital+2000; 
      capital+capital*.10; 
      Year+1; 
   end; 
run;
Here is what happens during each iteration of the DO loop:
  • 2000 is added to the value of Capital to reflect the annual deposit of $2,000.
  • 10% interest is added to Capital.
  • The value of Year is incremented by 1.
Because there is no index variable in the DO UNTIL statement, the variable Year is created in a sum statement to count the number of iterations of the DO loop. This program produces a data set that contains the single observation shown below. To accumulate more than $50,000 in capital requires 13 years (and 13 iterations of the DO loop).
Figure 15.4 SAS Data Set Work.Invest: Accumulation of More Than $50,000
SAS Data Set Work.Invest

Using the DO WHILE Statement

Like the DO UNTIL statement, the DO WHILE statement executes DO loops conditionally. You can use the DO WHILE statement to execute a DO loop while the expression is true.
Syntax, DO WHILE statement:
DO WHILE(expression);
...more SAS statements...
END;
expression is a 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, the DO loop never executes. For example, in the following program, because the value of Capital is initially zero, which is less than 50,000, the DO loop does not execute.
data work.invest; 
   do while(Capital>=50000); 
      capital+2000; 
      capital+capital*.10; 
      Year+1; 
   end; 
run;
Last updated: January 10, 2018
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.67.16