Search code examples
spss

SPSS: how to find text in text?


In my SPSS-dataset I have a string-variable QuestionA containing the answer to a certain question. However, instead of just one answer, it is possible to check more than one answer.

For example, if one checks answers 02, 05 and 07 it is saved in the variable QuestionA as the string "02;05;07".

I would like to create a variable for specific answer 02. Let's call that variable Answer02. It should contain a 0 if QuestionA does not contain 02 anywhere in its text, and a 1 if QuestionA actually contains 02 anywhere.

For me the catch lies in the fact that one could also check answer 01 as well which makes the answer contained in QuestionA 01;02. The answer should be generic, if possible, so that I can also create a variable Answer05 in similar fashion.


Solution

  • This should give you a flavour:

    DATA LIST FREE / Q (A9).
    BEGIN DATA
    "01" "02" "03" "01,02" "02,03" "04,05"
    END DATA.
    
    
    DO REPEAT A=A1 to A3 /B="01" "02" "03".
    IF CHAR.INDEX(Q,B)>0 A=1.
    END REPEAT.
    RECODE A1 to A3 (SYSMIS=0).
    EXE.