![]() Chapter Contents |
![]() Previous |
![]() Next |
| DO, Iterative |
| Valid: | in a DATA step |
| Category: | Control |
| Type: | Executable |
Syntax |
|
DO index-variable=specification-1 <, . . .
specification-n>;
. . . more SAS statements . . . |
| END; |
| Tip: | Unless you specify to drop it, the index variable is included in the data set that is being created. |
![[cautend]](../common/images/cautend.gif)
start <TO stop> <BY increment> <WHILE(expression) | UNTIL(expression)>
| Featured in: | Using Various Forms of the Iterative DO Statement |
| 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: | Using Various Forms of the Iterative DO Statement |
| 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: | Using Various Forms of the Iterative DO Statement |
| 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: | Using Various Forms of the Iterative DO Statement |
| See Also: | DO WHILE and DO UNTIL for more information. |
| Comparisons |
There are three other forms of the DO statement:
| Examples |
do month='JAN','FEB','MAR';
do count=2,3,5,7,11,13,17;
do i=5;
do i=var1-var5;
do i=var1, var2, var3;
do i='01JAN90'd,'25FEB90'd,'18APR90'd;
DO I=1,2,3 WHILE (condition);
/* correct coding */ do i=1 to 10; ...more SAS statements... end;
/* Warning - infinite looping can occur */ do i=1 to n by m; ...more SAS statements... if i=10 then leave; end; if i=10 then put 'EXITED LOOP';
flag=0; do i=1 to 10 until(flag); ...more SAS statements... if expression then flag=1; ...more SAS statements... end;
data iterate1; input x; exit=10; do i=1 to exit; y=x*normal(0); /* if y>25, */ /* changing i's value */ /* stops execution */ if y>25 then i=exit; output; end; datalines; 5 000 2500 ;
| See Also |
Statements:
|
![]() Chapter Contents |
![]() Previous |
![]() Next |
![]() Top of Page |
Copyright © 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.