SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

Creating Variables
Assigning Values Conditionally


Comparison and Logical Operators

When writing IF-THEN statements, you can use any of the following comparison operators:


OperatorComparison Operation
 = or eq equal to
^= or ne not equal to
 > or gt greater than
 < or lt less than
>= or ge greater than or equal to
<= or le less than or equal to
in equal to one of a list


Examples:
     if test<85 and time<=20
        then status='RETEST';
     if region in ('NE','NW','SW')
        then rate=fee-25;
     if target gt 300 or sales ge 50000
        then bonus=salary*.05;


You can also use these logical operators:


OperatorLogical Operation
& and
| or
^ or ~ not


Use the AND operator to execute the THEN statement if both expressions that are linked by AND are true.
     if status='OK' and type=3
        then count+1;
     if (age^=agecheck | time^=3)
        & error=1 then test=1;

Use the OR operator to execute the THEN statement if either expression that is linked by OR is true.

     if (age^=agecheck | time^=3)
        & error=1 then test=1;
     if status='S' or cond='E'
        then control='Stop';

Use the NOT operator to evaluate a false condition as true or a true condition as false.

     if (age^=agecheck | time^=3)
        & error=1 then test=1;
     if not(loghours<7500)
        then schedule='Quarterly';
     if region not in ('NE','SE')
        then bonus=200;

You must specify character values in quotes and in the same case as they appear in the data set.

     if status='OK' and type=3
        then count+1;
     if status='S' or cond='E'
        then control='Stop';
     if not(loghours<7500)
        then schedule='Quarterly';
     if not region in ('NE','SE')
        then bonus=200;

Logical comparisons that are enclosed in parentheses are evaluated as true or false before they are compared to other expressions. In the example below, the OR comparison in parentheses is evaluated before being compared to the AND operator.

SAS Statement with Operators


  back||next


Copyright © 2002 SAS Institute Inc., Cary, NC, USA. All rights reserved.
Terms of Use & Legal Information | Privacy Statement