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 15 comments

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

Hi Lisa!

Your data are not yet in SPSS? Are they in some other kind of file such as Excel, .txt or .csv? You can read in most of such file formats into SPSS with File -> Open data or Read text data. If you read the instructions in the dialog boxes carefully, you'll probably get it right in one or two attempts.

Regarding the second question, see z scores in SPSS.

Does that get you any further?

## By Lisa on October 15th, 2016

My question: My data set is 12. 11 are 0's and 1 is x>0. How do I enter that data into spss. I am trying to calculate a z score for the 12th x>0.

Thank you very much!

## By Rukwaro Simon on February 24th, 2016

Hi Ruben,

Thank you very much. I guess these two options are what i need but not sure which suites me. Preferably, i may choose to work with Filter. However, I will try them both and see how it works out. Regards

## By Ruben Geert van den Berg on February 24th, 2016

Hi Simon!

-For analyzing multiple datasets with similar variables but different cases, you'll usually first merge them with ADD FILES. You probably already did that.

-For analyzing groups of cases separately, one option is SPLIT FILE.

-A second option is FILTER.

If you use SPLIT FILE, your output will contain analysis 1 for each group, analysis 2 for each group and so on. If you use FILTER, you'll get all analyses for the first group, then all analyses for the second group and so on. So

which one to choose depends on which order you prefer for your output items, ok?## By Rukwaro Simon on February 23rd, 2016

At times i am analysis similar variables of two similar set of data but where i am expected to analyze them separately but entered in one data set. How do i filter to run frequencies for one set of data and not the other?