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 11.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 the DO loop does not execute because the value of Capital is initially zero, which is less than 50,000.
data work.invest; 
  do while(Capital>=50000); 
    capital+2000; 
    capital+capital*.10; 
    Year+1; 
  end; 
run;
Suppose you also want to limit the number of years you invest your capital to 10 years. You can add the UNTIL or WHILE expression to an iterative DO statement to further control the number of iterations. This iterative DO statement enables you to execute the DO loop until Capital is greater than or equal to 50000 or until the DO loop executes 10 times, whichever occurs first.
data work.invest; 
  do year=1 to 10 until (Capital>=50000);
    capital+2000; 
    capital+capital*.10; 
  end; 
run;
Figure 11.5 SAS Data Set Work.Invest: Executing DO Loop until Capital >=$50,000
SAS Data Set Work.Invest
In this case, the DO loop stops executing after 10 iterations, and the value of Capital never reaches 50000. If you increase the amount added to Capital each year to 4000, the DO loop stops executing after the eighth iteration when the value of Capital exceeds 50000.
data work.invest; 
  do year=1 to 10 until (Capital>=50000);
    capital+4000; 
    capital+capital*.10; 
  end; 
run;
Figure 11.6 SAS Data Set Work.Invest: Increase Amount Added to Capital Using a DO Loop
SAS Data Set Work.Invest
The UNTIL and WHILE expressions in an iterative DO statement function similarly to the DO UNTIL and DO WHILE statements. As shown in the following syntax, both statements require a valid SAS expression that is enclosed in parentheses.
DO index-variable=start TO stop BY increment UNTIL(expression);
DO index-variable=start TO stop BY increment WHILE(expression);
The UNTIL expression is evaluated at the bottom of the DO loop. Therefore, the DO loop always executes at least once. The WHILE expression is evaluated before the execution of the DO loop. As a result, if the condition is initially false, the DO loop never executes.
Last updated: August 23, 2018
..................Content has been hidden....................

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