ChiStats
ChiStats

Reputation: 11

How to determine whether a string of letters contains a certain letter in SPSS

I am working with data from a multiple choice survey question. Survey respondents were able to select options A, B, C and/or D. The data file simply lists the response(s) they chose, with no spaces, commas or semicolons in between. For example, the data for a respondent who selected options A and C would simply be "AC"; the data for a respondent who selected options A, B, C and D would be "ABCD".

I need to create 4 new variables that indicate whether the respondent selected each of the 4 choices (i.e., if they didn't select A then A = 0; if they did select A then A = 1, and so on). How do I do this with my string data, given that there are no spaces or commas between the responses? I would greatly appreciate your help!

Upvotes: 1

Views: 1541

Answers (1)

horace_vr
horace_vr

Reputation: 3176

The OP correctly hinted at the answer in his comment, but for the sake of clarity, and in the spirit of Stack Overflow, here is the full answer:

Assuming multi_answer is the string variable holding the survey answer:

DO REPEAT answers="A" "B" "C" D"
/vars=var1 to var4.
COMPUTE vars=0.
if CHAR.INDEX(multi_answer,answers)<>0 vars=1.
END REPEAT.
EXECUTE.

Edit: As per eli-k's comment, a more synthetic way is to use this inside the DO REPEAT END REPEAT structure: compute vars=char.index(multi_answer,answers)>0. so you don't need the compute vars=0. End of Edit.

Both codes will create 4 variables (var1 var2 var3 var4) with values of 0 or 1, as described in the question.

IBM SPSS Official help on the string functions (including CHAR.INDEX function) : https://www.ibm.com/support/knowledgecenter/pl/SSLVMB_23.0.0/spss/base/syn_transformation_expressions_string_functions.html

Also a possible duplicate of this :): SPSS: how to find text in text?

Upvotes: 2

Related Questions