Previous Topic: &BOOLEXPR

Next Topic: &CALL


BOOLEAN Expression Syntax

The Boolean expression must conform to the following syntax:

expression := term [ OR term … ]
term := factor [ AND factor … ]
factor := [ NOT … ] exp2
exp2 := ( expression ) | test
test := [ IGNORE { TRUE | FALSE } ]
        [ ANY | ALL ]
          loperand [ ,loperand,… ]
          operator
        [ ANY | ALL ]
          roperand [ ,roperand,… ]
loperand := operand
operator := = ==
            ¬= ¬==
            < <<
            > >>
            <= <<=
            >= >>=
            CONTAINS
            LIKE
            IS [ NOT ]
roperand := (For all operators except IS [ NOT ]):
              operand
              [ GENERIC | : operand ]
              [ { CHARACTER | NUMERIC | FOLD | NOFOLD } …]
              [ , … ]
            (For the IS [ NOT ] operators):
              type-name | &variable
              [ , … ]
type-name := 'constant' | “constant” | &variable | number
operand := ALPHA ALPHANUM ALPHANUMNAT
           BITLIST16 DATE1 DATE2
           DATE3 DATE4 DATE5
           DATE6 DATE7 DATE8
           DATE9 DATE10 DOMAIN
           DSN HEX MIXED
           MSGLVL N NAME
           NAME12 NAME256 NULL
           NUM REAL ROUTCDE
           SIGNNUM TIME1 TIME2
           TIME3 Y

Notes:

Examples: &BOOLEXPR

This example shows how you can use &BOOLEXPR to evaluate a complex condition that is beyond the capabilities of the &IF or &DOWHILE/&DOUNTIL NCL statements:

&R = &BOOLEXPR SUBCHAR=% DATA=+
%NAME = 'FRED SMITH' AND +
  %DOB < 700101 AND +
 (ANY %SKILL1, %SKILL2, %SKILL3, %SKILL4 = 'PROGRAMMER' +
OR +
   %RELATIVES CONTAINS 'Managing Director')
&IF &R = 1 &THEN &DO
:
&DOEND

This example shows how a dynamic expression is validated for syntactical correctness. The expression is contained in the NCL variables &EXPR1 to &EXPR10.

&R = &BOOLEXPR SUBCHAR=% EVAL=NO VARS=EXPR* +
 RANGE=(1,10)
&IF &R = INVALID &THEN &DO
:
&DOEND

This example shows how the dynamic expression of the last example is processed at a later time. The result is checked for the BAD value in case a non-numeric variable value or nonvalid type list variable was encountered. Otherwise, whether the result is true is checked.

&R = &BOOLEXPR SUBCHAR=% EVAL=YES VARS=EXPR* +
 RANGE=(1,10)
&IF &R = BAD &THEN &DO
   :
&DOEND
&ELSE
&IF &R = 1 &THEN &DO
   :
    &DOEND

Return Codes:

The &BOOLEXPR function typically returns a value that is assigned to the target variable on the assignment statement.

Syntax errors in the various operands, such as SUBCHAR or FOLD, result in the NCL process being abnormally terminated. Syntax errors in the Boolean expression or data validity errors never result in process termination.

The following return values are possible when using EVAL=YES (the default):

INVALID

The Boolean expression is not valid. &SYSMSG contains a descriptive message.

BAD

The Boolean expression is valid, but a variable referred to in the expression contained data that is not valid for the operator. Only the following conditions can return this value:

&SYSMSG contains a message describing the error.

0

The Boolean expression is valid and evaluated to false.

1

The Boolean expression is valid and evaluated to true.

The following return values are possible when using EVAL=NO:

INVALID

The Boolean expression is not valid. &SYSMSG contains a message with additional information.

VALID

The Boolean expression is valid. If EVAL=YES had been specified, evaluation would have been attempted.

No other values are returned.

Notes:

&BOOLEXPR processes in the following order:

  1. First, the operands of the &BOOLEXPR function itself are verified.

    That is, the EVAL, FOLD, SUBCHAR, VARS, RANGE, and DATA operands. Errors in these operands result in the NCL process being terminated.

  2. If the VARS/RANGE operands are used, the nominated variables are retrieved. Their values are concatenated together with blanks between them to form the source of the Boolean expression.
  3. The Boolean expression is then compiled into a parse tree. Errors during compilation result in the INVALID return value.
  4. If EVAL=NO was specified, execution is complete and the return value is VALID.
  5. If EVAL=YES was specified, the parse tree is interpreted. Variables are retrieved and tests are performed.

    Invalid variable values (numeric or type lists) result in the interpretation being terminated with a BAD return value.

    Evaluation of AND and OR lists is from left to right. The first false (for AND) or true (for OR) test will short-circuit the rest of the tests at that level.

    This left-to-right order means that you can specify, for example:

    %VAR IS SIGNNUM AND %VAR = 1:10

    This expression does not return a BAD value. Only if the value in &VAR is both numeric and 1 through 10, is the value 1 (true) returned. In all other cases, the value 0 (false) is returned.

    ANY/ALL evaluations are processed left-to-right, with the list entries on the right being performed for each list entry on the left in order.

    Note: Use of &variables with DATA= can cause too-early evaluation of variable contents, leading to syntax errors in the expression.

More information:

&TYPECHK

&IF

&DOWHILE

&DOUNTIL