Summary
A great way to dichotomize variables is with a single short compute command. This is also the fastest way to create multi variable filters.
How Does it Work?
When the structure COMPUTE A = B = C
is used, SPSS will evaluate whether (B = C)
is True or False for each case. The outcome variable A
will be the following
- If (B = C) is True, A = 1
- If (B = C) is False, A = 0
- If (B = C) can't be evaluated (due to missing values), A will be a system missing value
A nice extra here is that the outcome variable A
is perfect for using with FILTER.
Multiple Conditions
More complex conditions can be used in a similar vein, such as COMPUTE FLAG = V1 GT V2 AND V3 = 5.
. This would mean the following:
- If and only if (V1 is larger than V2) and (V3 is 5) will a case have 1 on
FLAG
. IfFLAG
is used as a filter, only these cases will stay active. - If one or both conditions are not true, 'FLAG' is 0.
- If there's any missing value on V1, V2 or V3, 'FLAG' is also missing.
- The logic is demonstrated by the syntax below.
SPSS Syntax Examples
*1. Create test data.
data list free/v1 v2.
begin data
1 6 '' 5 3 4 4 3 5 2 6 1
end data.
*2. Flag cases where v1 is greater than (gt) 2.
compute flag.1 = v1 gt 2.
*3. Flag cases where both v1 and v2 gt 2.
compute flag.2 = v1 gt 2 and v2 gt 2.
exe.
data list free/v1 v2.
begin data
1 6 '' 5 3 4 4 3 5 2 6 1
end data.
*2. Flag cases where v1 is greater than (gt) 2.
compute flag.1 = v1 gt 2.
*3. Flag cases where both v1 and v2 gt 2.
compute flag.2 = v1 gt 2 and v2 gt 2.
exe.
THIS TUTORIAL HAS 2 COMMENTS:
By Linda Martell on July 15th, 2015
Good
By Linda Martell on July 15th, 2015
VeryGood