Expressions

Expressions are grouped into three categories based on the types of values they can produce: logical, set, and scalar (that is, numeric or character).

Logical expressions test for a Boolean (true or false) condition. As in the DATA step, logical operators produce a value equal to either 0 or 1. A value of 0 represents a false condition, while a value of 1 represents a true condition.

Logical expression operators are not allowed in certain contexts due to syntactic considerations. For example, in the VAR statement a logical operator might indicate the start of an option. Enclose a logical expression in parentheses to use it in such contexts. The difference is illustrated by the output (Figure 4.6) of the following statements, where two variables, x and y, are declared with initial values. The PRINT statement and the EXPAND statement are used to check the initial values and the variable bounds, respectively.

proc optmodel;
   var x init 0.5 >= 0 <= 1;
   var y init (0.5 >= 0) <= 1;
   print x y;
   expand;

Figure 4.6 Logical Expression in the VAR Statement
x y
0.5 1

Var x >= 0 <= 1                                                                 
Var y <= 1                                                                      

Contexts that expect a logical expression also accept numeric expressions. In such cases zero or missing values are interpreted as false, and all non­zero non­missing numeric values are interpreted as true.

Set expressions return a set value. PROC OPTMODEL supports a number of operators that create and manipulate sets. See the section OPTMODEL Expression Extensions for a description of the various set expressions. Index-set syntax is described in the section Index Sets.

Scalar expressions are similar to the expressions in the DATA step except for PROC OPTMODEL extensions. PROC OPTMODEL provides an IF expression (described in the section IF-THEN/ELSE Expression). String lengths are assigned dynamically, so there is generally no padding or truncation of string values.

Table 4.3 shows the expression operators from lower to higher precedence (a higher precedence is given a larger number). Operators that have higher precedence are applied in compound expressions before operators that have lower precedence. The table also gives the order of evaluation that is applied when multiple operators of the same precedence are used together. Operators available in both PROC OPTMODEL and the DATA step have compatible precedences, except that in PROC OPTMODEL the NOT operator has a lower precedence than the relational operators. This means that, for example, NOT 1 < 2 is equal to NOT (1 < 2) (which is 0), rather than (NOT 1) < 2 (which is 1).

Table 4.3 Expression Operator Table

Precedence

Associativity

Operator

Alternates

Logic Expression Operators

1

Left to right

OR

| !

2

Unary

OR{index-set}

   

AND{index-set}

3

Left to right

AND

&

4

Unary

NOT

5

Left to right

<

LT

   

>

GT

   

<=

LE

   

>=

GE

   

=

EQ

   

=

NE = =

6

Left to right

IN

   

NOT IN

7

Left to right

WITHIN

   

NOT WITHIN

Set Expression Operators

11

 

IF l THEN s1 ELSE s2

 

12

Left to right

UNION

 
   

DIFF

 
   

SYMDIFF

 

13

Unary

UNION{index-set}

 

14

Left to right

INTER

 

15

Unary

INTER{index-set}

 

16

Left to right

CROSS

 

17

Unary

SETOF{index-set}

 
 

Right to left

..

TO

   

.. e BY

TO e BY

Scalar Expression Operators

21

 

IF l THEN e

 
   

IF l THEN e1 ELSE e2

 

22

Left to right

||

!!

23

Left to right

+ -

 

24

Unary

SUM{index-set}

 
   

PROD{index-set}

 
   

MIN{index-set}

 
   

MAX{index-set}

 

25

Left to right

* /

 

26

Unary

+ -

 
 

Right to left

><

 
   

<>

 
   

**

Primary expressions are the individual operands that are combined using the expression operators. Simple primary expressions can represent constants or named parameter and variable values. More complex primary expressions can be used to call functions or construct sets.

Table 4.4 Primary Expression Table

Expression

Description

identifier-expression

Parameter/variable reference; see the section Identifier Expressions

name (arg-list)

Function call; arg-list is 0 or more expressions separated by commas

n

Numeric constant

. or .c

Missing value constant

"string" or 'string'

String constant

{ member-list }

Set constructor; member-list is 0 or more scalar expressions or tuple expressions separated by commas

{ index-set }

Index set expression; returns the set of all index set members

/ members /

Set literal expression; compactly specifies a simple set value

( expression )

Expression enclosed in parentheses

< expr-list >

Tuple expression; used with set operations; contains one or more scalar expressions separated by commas