Search code examples
sas

Order of the relative operator in SAS?


I run the following SAS program, and I get the result=0, it suprising me!

data calculation;
    result = (3 gt 2 = 1); /* 0 */
run;
proc print;
run;

I get totally confused about it, could someone help me?

I have consult the order of the relation operator, and the docs told me it perform from left to right. but the 3 gt 2 = 1 in my opinion is True....


Solution

  • SAS has some special syntax not always found easily in guides or references. See Order of Evaluation in Compound Expressions

    Table Note 8 An exception to this rule occurs when two comparison operators surround a quantity. For example, the expression x<y<z is evaluated as (x<y) and (y<z).

    Commonly seen when testing if a value is in a range. For example:

      lower-bound <= value < upper-bound
    

    Your expression:

      expression-1 operator-1 expression-2 operator-2 expression-3
           3          gt           2          =            1
    

    is evaluated as

      expression-1 operator-1 expression-2 AND expression-2 operator-2 expression-3
           3          gt           2       AND      2          =            1
    

    ( (3 > 2) AND (2=1) ) resolves to 0

    In other situations, your intuition might also hit this wall "SAS does not guarantee short-circuit evaluation." In the narrative at the link there are "examples {that} show how SAS might use short-circuit evaluation at some times and not at others"