Roaring
Roaring

Reputation: 59

SPSS - Filter columns based on specific criteria

I have a dataset (See below) where I want to filter out any observations where there is only a 1 in the McDonalds column, such as for ID#3 (I do not want Mcdonalds in my analyses). I want to keep any observations where there is a 1 in other columns (eventhough there is a 1 in the McDonalds column - such as ID #1-2). I have tried using the select cases option, and just putting McDonalds=0, but this filters out any observations where there are 1s in the other columns as well. Below is a sample of my dataset, I actually have many more columns and was trying to avoid having to individually name every other column in the "Select Cases" option in SPSS. Would anyone be able to help me please? Thanks.

Data:

enter image description here

Upvotes: 2

Views: 750

Answers (2)

eli-k
eli-k

Reputation: 11310

To avoid naming each of the other columns separately you can use to in the syntax. Also, basically, you want to keep lines that have 1 in any of the other columns regardless of the value in the Mcdonald's column, so there is no need to mention it in the syntax.
So say for example that your column names are McDonalds, RedBull, var3, var4, var5, TacoBell, you could use either of these following options:

select if any(1, RedBull to TacoBell).

or this :

select if sum(RedBull to TacoBell)>1.

Note: using the to convention requires that the relevant variables be contiguous in the data.

Upvotes: 1

LuizZ
LuizZ

Reputation: 1044

You just need to add the "OR" operator (which is the vertical bar: |) between all the mentioned conditions.

So basically, you want to keep the cases when McDonalds = 0 | RedBull = 1 | TacoBell = 1.

You can either copy the above line into the Select cases -> If option, or write the following lines into the SPSS syntax file, replacing the DataSet1 for the name of your dataset:

DATASET ACTIVATE DataSet1.
USE ALL.
COMPUTE filter_$=(McDonalds = 0 | RedBull = 1 | TacoBell = 1).
VARIABLE LABELS filter_$ 'McDonalds = 0 | RedBull = 1 | TacoBell = 1 (FILTER)'.
VALUE LABELS filter_$ 0 'Not Selected' 1 'Selected'.
FORMATS filter_$ (f1.0).
FILTER BY filter_$.
EXECUTE.

Upvotes: 0

Related Questions