The Basics of DO Loops

The Basics of Using Grouping Statements and DO Groups

You can execute a group of statements as a unit by using DO groups.
To construct a DO group, you use the DO and END statements along with other SAS statements.
Syntax, DO group:
DO;
SAS statements
END;
  • The DO statement begins DO-group processing.
  • SAS statements between the DO and END statements are called a DO group and are executed as a unit.
  • The END statement terminates DO-group processing.
Tip:You can nest DO statements within DO groups.
You can use DO groups in IF-THEN/ELSE statements and SELECT groups to execute many statements as part of the conditional action.

Example: DO and END Statements

In this simple DO group, the statements between DO and END are performed only when TotalTime is greater than 800. If TotalTime is less than or equal to 800, statements in the DO group are not executed, and the program continues with the assignment statement that follows the appropriate ELSE statement.
data work.stresstest;
  set cert.tests;
  TotalTime=(timemin*60)+timesec;
  retain SumSec 5400;
  sumsec+totaltime;
  length TestLength $6 Message $20;
  if totaltime>800 then  
    do;
        TestLength='Long';
        message='Run blood panel';
    end;
    else if 750<=totaltime<=800 then TestLength='Normal';
    else if totaltime<750 then TestLength='Short';
run;
proc print data=work.stresstest;
run;
Output 11.1 PROC PRINT Output Work.StressTest (partial output)
Partial Output: PRINT Output Work.StressTest

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.
    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.
    Note: Unless you specify to drop it, the index variable is included in the data set that is being created.
  • 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, 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.

Example: Processing Iterative DO Loops

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 work.earn (drop=month); 
  set cert.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. In this example, the DO group Month is the index variable, 1 is the start-variable, and 12 is the stop variable.
data work.earnings (drop=month); 
  set cert.master; 
  Earned=0; 
  do month=1 to 12; 
    earned+(amount+earned)*(rate/12); 
  end;
  Balance=Amount+Earned; 
run; 
proc print data=work.earnings;
run;
Output 11.2 PROC PRINT Output of Work.Earnings
PROC PRINT of Work.Earnings
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
3.143.5.15