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.Five dichotomous variables combined into one.
Syntax for Creating Test Data
data list free/id.
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
*2. Create 5 dichotomous variables and label them.
do repeat v = v1 to v5.
compute v = rv.bernoulli(.5).
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
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).
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())
Combining Value Labels
- The final step starts with declaring a new (empty) string variable which we'll call
tmp(for "temporary"). Next we'll use DO REPEAT for looping over all dichotomous variables. The first country that was selected by a respondent will be passed into
- If more than one country was selected by someone, "
and" plus the country name are added to
tmp. We'll use CONCAT with RTRIM for this.
- If no countries were selected at all,
tmpstays empty. We chose to fill in "
(None selected)" for such cases.
- Next, we'll simply AUTORECODE
tmpand apply a variable label to it. Finally, we delete
tmpsince it's no longer needed.
Syntax for Combining Labels
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).
*If "tmp" is still empty after looping, none of the options was selected.
if char.length(rtrim(tmp)) = 0 tmp = '(None selected)'.
*5. Convert long string into numeric variable.
variable labels combined "Combined values on v1 to v5.".
*6. Delete "tmp".
delete variables tmp.