Logical expressions

Data transformation (IF), as well as case selection (SELECT IF) requires the specification of a : a case for which the expression is true is selected, a case for which the expression is false is rejected.

, like , can be quite simple or highly complex and can contain: variable names, constants, operators and functions.

Logical operators and functions

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:

SymbolAlternativeExplanation
= 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)

 

Logical expressions in menus

When using menus you will need to enter logical expression into dialog boxes. Here is what you get when selecting the IF 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.

Logical expressions and command language.

Several transformation commands use :

   IF (<log-expression>) <variable> = <expression> 
   DO IF (<log-expression>)
   SELECT IF (<log-expression>)