Home > Expression Operators > If Then Else Operators

If Then Else Operators

Returns ValueExpr corresponding to the first CondExpr for #IF or #ELSEIF which is 1 (true). If no CondExpr for #IF or #ELSEIF is true, then the ValueExpr for #ELSE is returned. If no #ELSE is specified, then #MV (missing value) is returned. In some cases, such as benefit formulas, processing will halt rather than return a missing value.

Syntax:

#IF CondExpr1 #THEN
  ValueExpr1
#ELSEIF CondExpr2 #THEN
  ValueExpr2

#ELSE ValueExpr3
#ENDIF

Where:

Example 1:

#IF PlanCode #IN (1,2) #THEN
  CAvgBft
#ELSEIF PlanCode=3 #THEN
  FAvgBft
#ENDIF

This expression returns:

It is equivalent to the expression:

[((PlanCode #in (1,2))* CAvgBft] + [(PlanCode=3) * FAvgBft]

except that it returns #MV (missing value) rather than 0 for records with PlanCode other than 1, 2, or 3. Also, if CAvgBft is #MV (missing value), it returns FAvgBft rather than #MV for records with PlanCode equal to 3 (and similarly if FAvgBft is #MV). Barring these differences with missing values, the choice to multiply Boolean values or to use if-then-else operators is an aesthetic one. The aesthetic difference is especially noticeable when the #IF and #ELSEIF conditions are not mutually exclusive (see example 2 below). In this case, if-then-else expresses the same logic in a more compact fashion. If-then-else, however, has little or no performance advantage.

Example 2:

If-then-else expressions can be nested, as in:

service := ValDate #YEARDIF DOH &
age := ValDate #YEARDIF DOB &

#IF(plan=1) #THEN
  ; RET ELIGIBILITY BUCKET FOR SALARIED PARTICIPANTS
  #IF (age >= 65) #AND (service >= 5) #THEN
    2 ; ‘UNRED’
  #ELSEIF (service >= 15) #AND (age + service >= 75) #THEN
    1 ; ‘RED’
  #ELSEIF age >= 60 #THEN
    5 ; ‘5YRS’
  #ELSEIF (service >= 10) #AND (age + service >= 65) #THEN
    5 ; ‘5YRS’
  #ELSEIF age >= 55 #THEN
    10 ; ‘10YRS’
  #ELSE
      0 ; ‘OTHER’
  #ENDIF
#ELSE
  ; RET ELIGIBILITY BUCKET FOR HOURLY PARTICIPANTS
  #IF age >= 60 #THEN
    2 ; ‘UNRED’
  #ELSEIF (service >= 25) #AND (age >= 55) #THEN
    2 ; ‘UNRED’
  #ELSEIF service >= 15 #THEN
    5 ; ‘RED’
  #ELSEIF (age >= 50) #OR (service >= 5) #THEN
    10 ; ‘10YRS’
  #ELSE
    0 ; ‘OTHER’
  #ENDIF
#ENDIF

 

Temporary variables and assignment

Note that you can’t embed the assignment of a temporary variable within an If-Then-Else statement; you need to embed the If-Then-Else statement within the temporary variable assignment, for example:

WRONG:

#IF FLAG = 1 #THEN [(TEMPVAR: = SOMEVALUE) #ELSE (TEMPVAR: = 0) #ENDIF] &….

RIGHT:

TEMPVAR: = (#IF FLAG=1 #THEN SOMEVALUE #ELSE 0 #ENDIF) &….

 

Selection Expressions

If-then-else operators are not a substitute for Selection Expressions, which select a subset of records to process. For example, if you want to set the Claims field for males to 10000 while leaving it unchanged for females, use:

By contrast, using the expression "#IF sex=1 #THEN 10000 #ENDIF" with no selection expression will set the Claims field to 10000 for males and to #MV (missing value) for females – overriding any existing values. Rather, the equivalent if-then-else expression would be "#IF sex=1 #THEN 10000 #ELSE Claims #ENDIF".