The Basics of DO Loops

DO Statement, Iterative Syntax

The iterative DO statement executes statements between the DO and END statements repetitively, based on the value of an index variable.
Syntax, DO statement, iterative:
DO index-variable=specification-1 <, ...specification-n>;
...more SAS statements...
END;
  • index-variable names a variable whose value governs execution of the DO group.
    Note: Unless you specify to drop it, the index variable is included in the data set that is being created.
    CAUTION:
    Avoid changing the index variable within the DO group.
    If you modify the index variable within the iterative DO group, you might cause infinite looping.
  • specification denotes an expression or series of expressions such as these:
    start <TO stop> <BY increment> <WHILE(expression) | UNTIL(expression)>
    The DO group is executed first with index-variable equal to start. The value of start is evaluated before the first execution of the loop.
    • start specifies the initial value of the index variable.
    • TO stop specifies the ending value of the index variable.
      Tip
      Any changes to stop made within the DO group do not affect the number of iterations. To stop iteration of a loop before it finishes processing, change the value of index-variable so that it passes the value of stop, or use a LEAVE statement to go to a statement outside the loop.
    • BY increment specifies a positive or negative number (or an expression that yields a number) to control the incrementing of index-variable.
      The value of increment is evaluated before the execution of the loop. If no increment is specified, the index variable is increased by 1. When increment is positive, start must be the lower bound, and stop, if present, must be the upper bound for the loop. If increment is negative, start must be the upper bound, and stop, if present, must be the lower bound for the loop.
    • WHILE(expression) | UNTIL(expression) evaluates, either before or after execution of the DO group, any SAS expression that you specify. Enclose the expression in parentheses.
      A WHILE expression is evaluated before each execution of the loop, so that the statements inside the group are executed repetitively while the expression is true. An UNTIL expression is evaluated after each execution of the loop, so that the statements inside the group are executed repetitively until the expression is true.
      Note: The order of the optional TO and BY clauses can be reversed.
      Note: When you use more than one specification, each one is evaluated before its execution.

Details

DO loops process a group of statements repeatedly rather than once. This can greatly reduce the number of statements required for a repetitive calculation. For example, these 12 sum statements compute a company's annual earnings from investments. Notice that all 12 statements are identical.
data finance.earnings; 
   set finance.master; 
   Earned=0; 
   earned+(amount+earned)*(rate/12); 
   earned+(amount+earned)*(rate/12); 
   earned+(amount+earned)*(rate/12); 
   earned+(amount+earned)*(rate/12); 
   earned+(amount+earned)*(rate/12); 
   earned+(amount+earned)*(rate/12); 
   earned+(amount+earned)*(rate/12); 
   earned+(amount+earned)*(rate/12); 
   earned+(amount+earned)*(rate/12); 
   earned+(amount+earned)*(rate/12); 
   earned+(amount+earned)*(rate/12); 
   earned+(amount+earned)*(rate/12); 
run;
In this program, each sum statement accumulates the calculated interest that is earned for an investment for one month. The variable Earned is created in the DATA step to store the earned interest. The investment is compounded monthly, meaning that the value of the earned interest is cumulative.
By contrast, a DO loop enables you to achieve the same results with fewer statements. In this case, the sum statement executes 12 times within the DO loop during each iteration of the DATA step.
data finance.earnings; 
   set finance.master; 
   Earned=0; 
   do month=1 to 12; 
      earned+(amount+earned)*(rate/12); 
   end; 
run; 
When creating a DO loop with the iterative DO statement, you must specify an index variable. The index variable stores the value of the current iteration of the DO loop. You can use any valid SAS name. The following brief examples show you the syntax.
DO index-variable=start TO stop BY increment; 
   ...more SAS statements... 
END;
Next, specify the conditions that execute the DO loop. A simple specification contains a start value, a stop value, and an increment value for the DO loop.
Do index-variable=start TO stop BY increment; 
   ...more SAS statements... 
END;
The start value specifies the initial value of the index variable.
DO index-variable=start TO stop BY increment; 
   ...more SAS statements... 
END;
The TO clause specifies the stop value. The stop value is the last index value that executes the DO loop.
DO index-variable=start TO stop BY increment; 
   ...more SAS statements... 
END;
The optional BY clause specifies an increment value for the index variable. Typically, you want the DO loop to increment by 1 for each iteration. If you do not specify a BY clause, the default increment value is 1.
DO index-variable=start TO stop BY increment; 
   ...more SAS statements... 
END;
For example, the specification below increments the index variable by 1, resulting in quiz values of 1, 2, 3, 4, and 5:
do quiz=1 to 5;
By contrast, the following specification increments the index variable by 2, resulting in rows values of 2, 4, 6, 8, 10, and 12:
do rows=2 to 12 by 2;
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
18.118.2.225