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

Expression | Type | Meaning | Returns |
---|---|---|---|

= 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

exe.

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.

***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 argument | Second argument | AND returns |
---|---|---|

True | Unknown | Unknown |

False | Unknown | False |

Unknown | Unknown | Unknown |

***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 argument | Second argument | OR returns |
---|---|---|

True | Unknown | True |

False | Unknown | Unknown |

Unknown | Unknown | Unknown |

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.

## This tutorial has 13 comments

## By Moses Adriko on September 10th, 2017

I like the procedures that are very easy to follow.

## By Ruben Geert van den Berg on October 26th, 2016

Hi Shashi! Thanks for your comment but this tutorial is about basic operators in SPSS. We wrote tons of other tutorials on descriptive and inferential statistics. Is there anything in particular you're looking for?

## By Shashi bala on October 26th, 2016

Tutorial lacks information regarding descripritive and inferential statistics.