SPSS: how to find text in text?

心不动则不痛 提交于 2021-01-29 04:20:27

问题


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.


回答1:


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.



回答2:


If you are just interested in that one case, this code is simpler.

COMPUTE Answer02=char.index(QuestionA, "02") > 0.



来源:https://stackoverflow.com/questions/39723961/spss-how-to-find-text-in-text

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!