SPSS tutorials website header logo SPSS TUTORIALS BASICS ANOVA REGRESSION FACTOR CORRELATION

SPSS Basic Operators

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.

SPSS Data Hospital

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

ExpressionTypeMeaningReturns
= or EQComparison operatorEqualTrue or false
<> or NE or ¬= or ~=Comparison operatorNot equalTrue or false
< or LTComparison operatorLess thanTrue or false
<= or LEComparison operatorLess than or equal toTrue or false
> or GTComparison operatorGreater thanTrue or false
>= or GEComparison operatorGreater than or equal toTrue or false
ANDLogical operatorEvaluate whether both conditions holdTrue or false
ORLogical operatorEvaluate whether one or both of the conditions holdTrue or false
NOTLogical operatorEvaluate whether condition does not holdTrue 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

compute flag_1 = doctor_rating = 5.
exe.

is perfectly valid syntax. It sets the values of flag_1 to

SPSS Compute Comparison

After running this syntax on our data, we see the result in data view as illustrated by the screenshot below.

SPSS Dichotomizing Variables

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.

*1. Flag cases having 5 on doctor_rating.

compute flag_1 = doctor_rating = 5.
exe.

*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.

*1. Flag cases whose doctor_rating is not equal to their nurse_rating.

compute flag_3 = doctor_rating <> nurse_rating.
exe.

*2. Flag cases whose first_name is not 'TIM'.

compute flag_4 = first_name <> 'TIM'.
exe.

SPSS LT Operator

An example of SPSS < operator is shown in the syntax below.

*Flag cases whose doctor_rating is less than their nurse_rating.

compute flag_5 = doctor_rating < nurse_rating.
exe.

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.

*Flag cases whose average rating is less than or equal to 2.

compute flag_6 = mean(doctor_rating to facilities_rating) <= 2.
exe.

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.

*Flag cases whose doctor_rating + nurse_rating is greater than 8.

compute flag_7 = doctor_rating + nurse_rating > 8.
exe.

SPSS GE Operator

The syntax below demonstrates a basic comparison using the >= operator.

*Flag cases whose nurse_rating is at least two points higher than their doctor_rating.

compute flag_8 = nurse_rating - doctor_rating >= 2.
exe.

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 argumentSecond argumentAND returns
TrueUnknownUnknown
FalseUnknownFalse
UnknownUnknownUnknown
*Flag cases whose doctor_rating and nurse_rating are both at least 4.

compute flag_9 = doctor_rating >= 4 and nurse_rating >= 4.
exe.

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 argumentSecond argumentOR returns
TrueUnknownTrue
FalseUnknownUnknown
UnknownUnknownUnknown

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.

*Flag cases having a 5 on either doctor_rating, nurse_rating or both.

compute flag_10 = doctor_rating = 5 or nurse_rating = 5.
exe.

*Alternative with ANY. Note that missing values are handled slightly differently.

compute flag_11 = any(5,doctor_rating, nurse_rating).
exe.

SPSS NOT Operator

SPSS NOT operator reverses the outcome of other (combinations of) comparisons. An example is shown below.

*Flag cases who didn't rate anything with 5 points.

compute flag_12 = not(any(5,doctor_rating to nurse_rating)).
exe.

Tell us what you think!

*Required field. Your comment will show up after approval from a moderator.

THIS TUTORIAL HAS 15 COMMENTS: