DO Statement, Iterative


Executes statements between DO and END repetitively based on the value of an index variable

Valid: in a DATA step

Category: Control

Type: Executable

Syntax

DO index-variable = specification-1 <,... specification-n >;

  • ... more SAS statements ...

END;

Arguments

index-variable

  • names a variable whose value governs execution of the DO group . The index -variable argument is required.

  • Tip: 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 may cause infinite looping.

specification

  • denotes an expression or a series of expressions in this form

    • start <TO stop > <BY increment >

      • <WHILE( expression ) UNTIL( expression )>

  • Requirement: The iterative DO statement requires at least one specification argument.

  • Tip: The order of the optional TO and BY clauses can be reversed .

  • Tip: When you use more than one specification , each one is evaluated prior to its execution.

start

  • specifies the initial value of the index variable.

  • Restriction: When it is used with TO stop or BY increment , start must be a number or an expression that yields a number.

  • Explanation: When it is used without TO stop or BY increment , the value of start can be a series of items expressed in this form:

    • item-1 <, . . . item-n >;

    The items may be either all numeric or all character constants, or they may be variables . Enclose character constants in quotation marks. The DO group is executed once for each value in the list. If a WHILE condition is added, it applies only to the item that it immediately follows .

    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.

  • Featured in: Example 1 on page 1146

TO stop

  • optionally specifies the ending value of the index variable.

  • Restriction: Stop must be a number or an expression that yields a number.

  • Explanation: When both start and stop are present, execution continues (based on the value of increment ) until the value of index-variable passes the value of stop . When only start and increment are present, execution continues (based on the value of increment ) until a statement directs execution out of the loop, or until a WHILE or UNTIL expression that is specified in the DO statement is satisfied. If neither stop nor increment is specified, the group executes according to the value of start . The value of stop is evaluated before the first execution of the loop.

  • 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.

  • Featured in: Example 1 on page 1146

BY increment

  • optionally specifies a positive or negative number (or an expression that yields a number) to control the incrementing of index-variable .

  • Explanation: The value of increment is evaluated prior to the execution of the loop. Any changes to the increment that are made within the DO group do not affect the number of iterations. 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.

  • Featured in: Example 1 on page 1146

WHILE( expression ) UNTIL( expression )

  • optionally evaluates , either before or after execution of the DO group, any SAS expression that you specify. Enclose the expression in parentheses.

  • Restriction: A WHILE or UNTIL specification affects only the last item in the clause in which it is located.

  • Explanation: 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.

  • Featured in: Example 1 on page 1146

  • See Also: DO WHILE Statement on page 1149 and DO UNTIL Statement on page 1148 for more information.

Comparisons

There are three other forms of the DO statement:

  • The DO statement, the simplest form of DO-group processing, designates a group of statements to be executed as a unit, usually as a part of IF-THEN/ELSE statements.

  • The DO UNTIL statement executes statements in a DO loop repetitively until a condition is true, checking the condition after each iteration of the DO loop.

  • The DO WHILE statement executes statements in a DO loop repetitively while a condition is true, checking the condition before each iteration of the DO loop.

Examples

Example 1: Using Various Forms of the Iterative DO Statement

  • These iterative DO statements use a list of items for the value of start :

    • do month='JAN','FEB','MAR';

    • do count=2,3,5,7,11,13,17;

    • do i=5;

    • do i=var1, var2, var3;

    • do i='01JAN2001'd,'25FEB2001'd,'18APR2001'd;

  • These iterative DO statements use the start TO stop syntax:

    • do i=1 to 10;

    • do i=1 to exit;

    • do i=1 to x-5;

    • do i=1 to k-1, k+1 to n;

    • do i=k+1 to n-1;

  • These iterative DO statements use the BY increment syntax:

    • do i=n to 1 by -1;

    • do i=.1 to .9 by .1, 1 to 10 by 1,

      • 20 to 100 by 10;

    • do count=2 to 8 by 2;

  • These iterative DO statements use WHILE and UNTIL clauses:

    • do i=1 to 10 while(x < y);

    • do i=2 to 20 by 2 until((x/3) > y);

    • do i=10 to 0 by -1 while(month= JAN );

  • In this example, the DO loop is executed when I=1 and I=2; the WHILE condition is evaluated when I=3, and the DO loop is executed if the WHILE condition is true.

     DO I=1,2,3 WHILE (condition); 

Example 2: Using the Iterative DO Statement without Infinite Looping

In each of the following examples, the DO group executes ten times. The first example demonstrates the preferred approach.

 /* correct coding */  do i=1 to 10;     ...  more SAS statements  ...  end; 

The next example uses the TO and BY arguments.

 do i=1 to n by m;     ...  more SAS statements  ...     if i=10 then leave;  end;  if i=10 then put EXITED LOOP; 

Example 3: Stopping the Execution of the DO Loop

  • In this example, setting the value of the index variable to the current value of EXIT causes the loop to terminate.

     data iterate1;      input x;      exit=10;      do i=1 to exit;         y=x*normal(0);            /* if y>25,             */            /* changing is value   */            /* stops execution      */         if y>25 then i=exit;         output;     end;     datalines;  5  000  2500  ; 

See Also

Statements:

  • ARRAY Statement on page 1100

  • Array Reference Statement on page 1105

  • DO Statement on page 1143

  • DO UNTIL Statement on page 1148

  • DO WHILE Statement on page 1149

  • GO TO Statement on page 1210




SAS 9.1 Language Reference Dictionary, Volumes 1, 2 and 3
SAS 9.1 Language Reference Dictionary, Volumes 1, 2 and 3
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 704

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net