syntaxspss

Concat Valuelabels with IF condition in SPSS


I have 15 variables created from another question's response. If they answered the question a certain way it is a 1 otherwise it is a SYSMIS. I have another column called ValuesCount. If there is only 1 input from the 15 variables I just label ValuesCount as that variable's label. Now I need to figure out how to combine all labels if there is more than one selected. There can be 1-15 in the ValuesCount field. I think the Concat + ValueLabel is what will get the answer, but I've tried several ways of doing this, but can't seem to get it to work. This was the code I started with to play and see if I could figure it out, but no luck.


* Define variable labels (headers).
VARIABLE LABELS Var1 'Variable 1 label'.
VARIABLE LABELS Var2 'Variable 2 label'.

* Concatenate headers with IF statement.
COMPUTE ConcatHeaders = STRING('Variable 1: ', Var1) + IF (Var1 > 2) STRING(' (Greater than 2)') ELSE '' END IF + ', Variable 2: ' + STRING(Var2).

* Show variable labels and concatenated headers.
EXECUTE.

Solution

  • The following loop will create the variable you are trying to make, and solve the "+" problem at the beginning of the text:

    String bought(A120).
    String plus(A1).
    do repeat vrnm=SeekersMatch FrugalMatch CreativesMatch
             /vrlb='SeekersMatch' 'FrugalMatch' 'CreativesMatch'.
       if bought<>"" plus="+".
       if vrnm bougt=concat(rtrim(bougt), plus, vrlb).
    end repeat.