Logical expressions | SPSS |
Resources ? | Back |
Data transformation (IF), as well as case selection (SELECT IF) requires the specification of a logical expression: a case for which the expression is true is selected, a case for which the expression is false is rejected.
Logical expressions, like
numeric expressions, can be quite
simple or highly complex and can contain: variable names, constants,
operators and functions.
[numeric expressions]
Logical expressions usually contain a logical operator, for instance:
Gender = 2 | True if gender equals 2 |
Age> 20 | True if a case has an age larger than 20 |
Age > 20 & Gender =2 | True if age is larger than 20 and at the same time Gender is equal to 2. |
country = "CH" | True if in the (string) variable country a case has a value of "CH" (note that the quotes are required for string values) |
Generally speaking the form of a logical expression is:
<expression> <operator> <expression>
A <expression> can just be a variable name or a constant or an arbitrary complex <expression>. Use parentheses to clarify the hierarchy of operations in complex expressions.
The main logical operators are:
Symbol | Alternative | Explanation |
---|---|---|
= | EQ | Equal |
~= | NE | Not equal |
< | LT | less than |
<= | LE | equal or less |
> | GT | greater than |
>= | GE | greater than or equal |
& | AND | (logical) and |
| | OR | Logical or |
~ | NOT | negation |
In addition to these elements, there are a number of useful logical functions, namely: [See the SPSS documentation for more.]
RANGE(exp,low,high) | True if <exp> is between <low> and <high>. You can specify several pairs of <low><high> elements in the same function call. | RANGE(AGE,20,40) |
ANY(exp,val[,val]) | True if <exp> yields any of the listed <val> values | ANY(PARTY,1,4,6) |
MISSING(var) | True if a case is missing (user or SYSMIS) | MISSING(PARTY) |
SYSMIS | True if a case is SYSMIS. | SYMIS(PARTY) |
When using menus you will need to enter logical expression
into dialog boxes. Here is what you get when selecting the
button
to
specify a conditional transformation.
A similar but dialog is shown by
to select (filter) observations for analysis).
As you can see you can either type in the expression directly or compose it by using the various elements present: buttons to select operators etc, function lists to find and insert a function, and the variable list to find and insert variables.
Several transformation commands use Logical expressions:
IF (<log-expression>) <variable> = <expression> DO IF (<log-expression>) SELECT IF (<log-expression>)© Copyright, Eugene Horber, U. of Geneva and FORS, 2024