SPSS basic operators are mainly used with IF, DO IF and COMPUTE. They work mostly as you'd expect but they do have a couple of surprises in store. No worries, we'll walk you through. We'll demonstrate how to get things done on the last 5 variables in hospital.sav.
Before jumping into SPSS operators, we'll first set 6 as a user missing value for all relevant variables by running missing values doctor_rating to facilities_rating (6).
SPSS Basic Operators
|= or EQ||Comparison operator||Equal||True or false|
|<> or NE or ¬= or ~=||Comparison operator||Not equal||True or false|
|< or LT||Comparison operator||Less than||True or false|
|<= or LE||Comparison operator||Less than or equal to||True or false|
|> or GT||Comparison operator||Greater than||True or false|
|>= or GE||Comparison operator||Greater than or equal to||True or false|
|AND||Logical operator||Evaluate whether both conditions hold||True or false|
|OR||Logical operator||Evaluate whether one or both of the conditions hold||True or false|
|NOT||Logical operator||Evaluate whether condition does not hold||True or false|
True, False and Unknown
SPSS operators can return three values: true, false and unknown. SPSS uses 1 to indicate “true” and 0 to indicate “false”. This seemingly trivial fact opens up some surprising shortcuts. For example
is perfectly valid syntax. It sets the values of flag_1 to
- 0 for cases not having 5 on doctor_rating;
- 1 for cases having 5 on doctor_rating;
- a system missing value for cases having a missing value on doctor_rating.
After running this syntax on our data, we see the result in data view as illustrated by the screenshot below.
Like so, this is a great shortcut for dichotomizing variables and we'll use it throughout this tutorial and many others.
Missing Values in SPSS Operators
SPSS operators will return a system missing value (meaning “unknown”) when a missing value is encountered in a basic operator. This holds for user missing values (which are not really missing or unknown) as well. A surprising result is that compute flag_2 = doctor_rating = 6. returns system missing values for cases having “6” (a user missing value) on doctor_rating. Even though it's clear that doctor_rating = 6 indeed for such cases, 6 being a missing value still causes SPSS to return “unknown” for this comparison.
SPSS EQ Operator
Using SPSS = operator is straightforward. In case of string variables, keep in mind that the string values must be quoted and the comparison is case sensitive. The syntax below gives two examples.
compute flag_1 = doctor_rating = 5.
*2. Flag cases whose first_name = 'TIM'.
compute flag_2 = first_name = 'TIM'.
*3. Move cases called 'TIM' to top of file.
sort cases by flag_2 (d).
SPSS NE Operator
For SPSS <> operator, the same basics hold as for the = operator. A numeric and a string example are given below.
compute flag_3 = doctor_rating <> nurse_rating.
*2. Flag cases whose first_name is not 'TIM'.
compute flag_4 = first_name <> 'TIM'.
SPSS LT Operator
An example of SPSS < operator is shown in the syntax below.
compute flag_5 = doctor_rating < nurse_rating.
SPSS LE Operator
The syntax below demonstrates SPSS <= operator. Note that we can use a statistical function (in this case MEAN) in such a comparison.
compute flag_6 = mean(doctor_rating to facilities_rating) <= 2.
SPSS GT Operator
The example below uses a basic addition in a comparison using SPSS > operator. Do keep in mind that numeric functions return system missing values when one or more of their arguments are missing values.
compute flag_7 = doctor_rating + nurse_rating > 8.
SPSS GE Operator
The syntax below demonstrates a basic comparison using the >= operator.
compute flag_8 = nurse_rating - doctor_rating >= 2.
SPSS AND Operator
SPSS AND operator combines two logical expressions. It returns “true” if both of its arguments are true. The schedule below shows its outcomes when one or both of its arguments are unknown.
|First argument||Second argument||AND returns|
compute flag_9 = doctor_rating >= 4 and nurse_rating >= 4.
SPSS OR Operator
SPSS OR operator returns “true” if at least one of its arguments are true. The schedule below holds the outcomes when one or both arguments are unknown.
|First argument||Second argument||OR returns|
If you need more than two OR operators in one command, ANY and RANGE are often better alternatives.Do note that missing values may be handled slightly differently by the latter two functions.
compute flag_10 = doctor_rating = 5 or nurse_rating = 5.
*Alternative with ANY. Note that missing values are handled slightly differently.
compute flag_11 = any(5,doctor_rating, nurse_rating).
SPSS NOT Operator
SPSS NOT operator reverses the outcome of other (combinations of) comparisons. An example is shown below.
compute flag_12 = not(any(5,doctor_rating to nurse_rating)).