SPSS Tutorials

BASICS REGRESSION T-TEST CHI-SQUARE TEST ANOVA

Combine Dichotomous Variables

Summary

Two or more dichotomous variables can be combined into a single variable without losing information. The basic trick here is to concatenate their value labels into a string variable and use AUTORECODE. We'll start by creating some test data for this by running the syntax below.

SPSS Combine Dichotomous VariablesFive dichotomous variables combined into one.

Syntax for Creating Test Data

*1. Create 25 cases.

data list free/id.
begin data
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
end data.

*2. Create 5 dichotomous variables and label them.

do repeat v = v1 to v5.
compute v = rv.bernoulli(.5).
end repeat.
exe.

variable labels v1 "Country visited: Italy".
variable labels v2 "Country visited: France".
variable labels v3 "Country visited: Spain".
variable labels v4 "Country visited: USA".
variable labels v5 "Country visited: Greece".

value labels v1 to v5 0 'Not selected' 1 'Selected'.

Adjusting Value Labels

Part of the trick is resetting the value labels. Since this is not easily reversible, you could first use SPSS Clone Variables Tool and use the clones for the rest of the procedure. We'll now use each country's name as the value label for value 1. Importantly, we do not label any of the other values. For this case we'll only use the country names as value labels. You could do this either manually or by using a tiny Python script. The syntax below shows both methods.

Syntax for Resetting Value Labels

*3A. Reset value labels. Use country name for 1, no value label for other values.

value labels v1 1 'Italy'.
value labels v2 1 'France'.
value labels v3 1 'Spain'.
value labels v4 1 'USA'.
value labels v5 1 'Greece'.

*3B. Alternative for 3A. Have Python reset value labels (same result as 3A).

begin program.
import spss,spssaux
dict = spssaux.VariableDict(caseless = True)
vars = dict.expand('v1 to v5') # Specify variables here.
for var in vars:
    varLab = dict[var].VariableLabel
    # Value label = variable label characters after ': ' (just country name)
    valLab = varLab[varLab.rfind(':') + 2:]
    spss.Submit("value labels %(var)s 1 '%(valLab)s'."%locals())
end program.

Combining Value Labels

Syntax for Combining Labels

*4. Concatenate value labels into single long string variable.

string tmp(a1000).

do repeat v = v1 to v5.

*Already value label(s) in "tmp" and current value label not empty? Add " and " + value label to "tmp".
if char.length(rtrim(tmp)) ne 0 and char.length(valuelabels(v)) gt 0 tmp = concat(rtrim(tmp),' and ',valuelabels(v)).

*If "tmp" is empty, just pass (possibly empty) value label into it.
if char.length(rtrim(tmp)) = 0 tmp = valuelabels(v).

end repeat.

*If "tmp" is still empty after looping, none of the options was selected.
if char.length(rtrim(tmp)) = 0 tmp = '(None selected)'.
exe.

*5. Convert long string into numeric variable.

autorecode tmp
/into combined.

variable labels combined "Combined values on v1 to v5.".

*6. Delete "tmp".

delete variables tmp.

Previous tutorial: Compute A = B = C

Next tutorial: SPSS – Open CSV Data File

Let me know what you think!

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

This tutorial has 1 comment!