The OPTMODEL Procedure

DO Statement, Iterative

DO name = specification-1 [, ...specification-n] ; statement(s) ; END;

The iterative DO statement assigns the values from the sequence of specification items to a previously declared parameter or variable, name. The specified statement sequence is executed after each assignment. This statement corresponds to the iterative DO statement of the DATA step.

Each specification provides either a single number or a single string value, or a sequence of such values. Each specification takes the following form:

expression [ WHILE( logic-expression ) | UNTIL( logic-expression ) ]

The expression in the specification provides a single value or set of values to assign to the target name. Multiple values can be provided for the loop by giving multiple specification items that are separated by commas. For example, the following code outputs the values 1, 3, and 5:

  
    proc optmodel; 
       number i; 
       do i=1,3,5; 
          put i; 
       end;
 

The same effect can be achieved with a single range expression in place of the explicit list of values, as in the following code:

    proc optmodel;
       number i;
       do i=1 to 5 by 2;
          put 'value of i assigned by the DO loop = ' i;
          i=i**2;
          put 'value of i assigned in the body of the loop = ' i;
       end;
 

The output of this code is shown in Output 6.14.

                                                                                
                                                                                 
                                                                                 
 value of i assigned by the DO loop = 1                                          
 value of i assigned in the body of the loop = 1                                 
 value of i assigned by the DO loop = 3                                          
 value of i assigned in the body of the loop = 9                                 
 value of i assigned by the DO loop = 5                                          
 value of i assigned in the body of the loop = 25                                
 


Figure 6.14: DO Loop: Name Parameter Unaffected

Note that unlike the DATA step, a range expression requires the limit to be specified. Additionally the BY part, if any, must follow the limit expression. Moreover, while the name parameter can be reassigned in the body of the loop, the sequence of values that is assigned by the DO loop is unaffected.

Expression can also be an expression that returns a set of numbers or strings. For example, the following code produces the same output as the previous code but uses a set parameter value:

  
    proc optmodel; 
       set s = {1,3,5}; 
       number i; 
       do i = s; 
          put i; 
       end;
 

Each specification can include a WHILE or UNTIL clause. A WHILE or UNTIL clause applies to the expression that immediately precedes the clause. The sequence that is specified by an expression can be terminated early by a WHILE or UNTIL clause. A WHILE logic-expression is evaluated for each sequence value before the nested statements. If the logic-expression returns a false (zero or missing) value, then the current sequence is terminated immediately. An UNTIL logic-expression is evaluated for each sequence value after the nested statements. The sequence from the current specification is terminated if the logic-expression returns a true value (nonzero and nonmissing). After early termination of a sequence due to a WHILE or UNTIL expression, the DO loop execution continues with the next specification, if any.

To demonstrate use of the WHILE clause, the following code outputs the values 1, 2, and 3. In this case the sequence of values from the set s is stopped when the value of i reaches 4.

  
    proc optmodel; 
       set s = {1,2,3,4,5}; 
       number i; 
       do i = s while(i NE 4); 
          put i; 
       end;
 

Previous Page | Next Page | Top of Page