The OPTMODEL Procedure

NUMBER, STRING, and SET Parameter Declarations

NUMBER parameter-decl [, ...parameter-decl] ;
STRING parameter-decl [, ...parameter-decl] ;
SET [ <scalar-type, ...scalar-type >] parameter-decl [, ...parameter-decl] ;

Parameters provide names for constants. Parameters are declared by specifying the parameter type followed by a list of parameter names. Declarations of parameters that have NUMBER or STRING types start with a scalar-type specification:

NUMBER | NUM
STRING | STR

The NUM and STR keywords are abbreviations for the NUMBER and STRING keywords, respectively.

The declaration of a parameter that has the set type begins with a set-type specification:

SET [ <scalar-type, ...scalar-type >]

In a set-type declaration, the SET keyword is followed by a list of scalar-type items that specify the member type. A set with scalar members is specified with a single scalar-type item. A set with tuple members has a scalar-type item for each tuple element. The scalar-type items specify the types of the elements at each tuple position.

If the SET keyword is not followed by a list of scalar-type items, then the set type is determined from the type of the initialization expression. The declared type defaults to SET<NUMBER> if no initialization expression is given or if the expression type cannot be determined.

For any parameter type, the type declaration is followed by a list of parameter-decl items that specify the names of the parameters to declare. In a parameter-decl item the parameter name can be followed by an optional index specification and any necessary options, as follows:

name [ { index-set } ] [ parameter-option(s) ]

The parameter name and index-set can be followed by a list of parameter-options. Dummy parameters declared in the index-set can be used in the parameter-options. The parameter options can be specified with the following forms:

= expression
This option provides an explicit value for each parameter location. In this case the parameter acts like an alias for the expression value.

INIT expression


This option specifies a default value that is used when a parameter value is required but no other value has been supplied. For example:

  
    number n init 1; 
    set s init {'a', 'b', 'c'};
 


PROC OPTMODEL evaluates the expression for each parameter location the first time the parameter needs to be resolved. The expression is not used when the parameter already has a value.

= [ initializer(s) ]
This option provides a compact means to define the values for an array, in which each array location value can be individually specified by the initializers.

The =expression parameter option defines a parameter value by using a formula. The formula can refer to other parameters. The parameter value is updated when the referenced parameters change. The following example shows the effects of the update:

  
    proc optmodel; 
       number n; 
       set<number> s = 1..n; 
       number a{s}; 
       n = 3; 
       a[1] = 2;    /* OK */ 
       a[7] = 19;   /* error, 7 is not in s */ 
       n = 10; 
       a[7] = 19;   /* OK now */
 


In the preceding example the value of set s is resolved for each use of array a that has an index. For the first use of a[7], the value 7 is not a member of the set s. However, the value 7 is a member of s at the second use of a[7].

The INIT expression parameter option specifies a default value for a parameter. The following example shows the usage of this option:

    proc optmodel;
       num a{i in 1..2} init i**2;
       a[1] = 2;
       put a[*]=;
 


When the value of a parameter is needed but no other value has been supplied, the default value specified by INIT expression is used, as shown in Output 6.7.

                                                                                
                                                                                 
                                                                                 
 a[1]=2 a[2]=4                                                                   
 


Figure 6.7: INIT Option: Output

Note: Parameter values can also be read from files or specified with assignment statements. However, the value of a parameter that is assigned with the =expression or =[ initializer(s)] forms can be changed only by modifying the parameters used in the defining expressions. Parameter values specified by the INIT option can be reassigned freely.

Initializing Arrays

Arrays can be initialized with the =[ initializer(s)] form. This form is convenient when array location values need to be individually specified. This form of initialization is used in the following code:

    proc optmodel;
       number a{1..3} = [5 4 7];
       put a[*]=;
 


Each array location receives a different value, as shown in Output 6.8.

                                                                                
                                                                                 
                                                                                 
 a[1]=5 a[2]=4 a[3]=7                                                            
 


Figure 6.8: Array Initialization

Each initializer takes the following form:

[ [ index ] ] value

The value specifies the value of an array location and can be a numeric or string constant, a set literal, or an expression enclosed in parentheses.

In array initializers, string constants can be specified using quoted strings. When the string text follows the rules for a SAS name, the text can also be specified without quotation marks. String constants that begin with a digit, contain blanks, or contain other special characters must be specified with a quoted string.

As an example, the following code defines an array parameter that could be used to map numeric days of the week to text strings:

  
    proc optmodel; 
       string dn{1..5} = 
              [Monday Tuesday Wednesday Thursday Friday];
 

The optional index in square brackets specifies the index of the array location to initialize. The index specifies one or more numeric or string subscripts. The subscripts allow the same syntactic forms as the value items. Commas can be used to separate index subscripts. For example, location a[1,'abc'] of an array a could be specified with the index [1 abc]. The following example initializes just the diagonal locations in a square array:

  
    proc optmodel; 
       number m{1..3,1..3} = [[1 1] 0.1 [2 2] 0.2 [3 3] 0.3];
 

An index does not need to specify all the subscripts of an array location. If the index begins with a comma, then only the right-most subscripts of the index need to be specified. The preceding subscripts are supplied from the index that was used by the preceding initializer . This can simplify the initialization of arrays that are indexed by multiple subscripts. For example, you can add new entries to the matrix of the previous example by using the following code:

  
    proc optmodel; 
       number m{1..3,1..3} = [[1 1] 0.1            [,3] 1 
                                        [2 2] 0.2  [,3] 2 
                                                  [3 3] 0.3];
 

The spacing shows the layout of the example array. The previous example was updated by initializing two more values at m[1,3] and m[2,3].

If an index is omitted, then the next location in the order of the array's index set is initialized. If the index set has multiple index-set-items , then the rightmost indices are updated before indices to the left are updated. At the beginning of the initializer list, this is the first member of the index set. The index set must use a range expression to avoid unpredictable results when an index value is omitted.

The initializers can be followed by commas. The use of commas has no effect on the initialization. The comma can be used to clarify layout. For example, the comma could separate rows in a matrix.

Not every array location needs to be initialized. The locations without an explicit initializer are set to zero for numeric arrays, set to an empty string for string arrays, and set to an empty set for set arrays.

Note: An array location must not be initialized more than once during the processing of the initializer list.

Previous Page | Next Page | Top of Page