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 operatorOR
. - 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 specifyX EQ 1 OR X EQ 2
. - The
ANY
andRANGE
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.
|
* 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.