AND and OR logical operators
Two or more relations can be logically joined using the logical operators AND and OR. Logical operators combine relations according to the following rules:
- The ampersand (&) symbol is a valid substitute for the logical operator AND. The vertical bar ( | ) is a valid substitute for the logical operator OR.
- Only one logical operator can be used to combine two relations. However, multiple relations can be combined into a complex logical expression.
- Regardless of the number of relations and logical operators used to build a logical expression, the result is either true, false, or indeterminate because of missing values.
- Operators or expressions cannot be implied. For example, X EQ 1 OR 2 is illegal; you must specify X EQ 1 OR X EQ 2.
- The ANY and RANGE functions can be used to simplify complex expressions.
AND . Both relations must be true for the complex expression to be true.
OR . If either relation is true, the complex expression is true.
The following table lists the outcomes for AND and OR combinations.
Expression | Outcome | Expression | Outcome |
---|---|---|---|
true AND true |
= true |
true OR true |
= true |
true AND false |
= false |
true OR false |
= true |
false AND false |
= false |
false OR false |
= false |
true AND missing |
= missing |
true OR missing |
= true* |
missing AND missing |
= missing |
missing OR missing |
= missing |
false AND missing |
= false* |
false OR missing |
= missing |
* Expressions where the outcome can be evaluated with incomplete information. See the topic Missing values in logical expressions for more information.
Example
DATA LIST FREE /var1 var2 var3.
BEGIN DATA
1 1 1
1 2 1
1 2 3
4 2 4
END DATA.
SELECT IF var1 = 4 OR ((var2 > var1) AND (var1 <> var3)).
- Any case that meets the first condition--var1 = 4--will be selected, which in this example is only the last case.
- Any case that meets the second condition will also be selected. In this example, only the third case meets this condition, which contains two criteria: var2 is greater than var1 and var1 is not equal to var3.