Loop through items and sum items in SPSS

断了今生、忘了曾经 提交于 2020-01-25 11:23:12

问题


I have two sets of variables called ITEM 1 to ITEM 47, and another called L1 to L47. What I want to do is to calculate the sum of Ls if any ITEM#i=1. What I wrote is as following:

COMPUTE LSUM=0. LOOP #i=1 to 47.
IF (ITEM(#i)=1) LSUM=LSUM+L(#i). END LOOP.

But I got an error message saying the characters do not match any existing function or vector. What should I do then? Your inputs will be very appreciated.

Thanks.

Sincerely, Lucy


回答1:


COMPUTE LSUM=0.
exe.

vector vitems = ITEM 1 to ITEM 47.
vector vl = L1 to L47.

LOOP #vecid = 1 to 47.
do IF (  vitems(#vecid) eq 1 and not missing(vl(#vecid))  ).
compute LSUM=LSUM+vl(#vecid).
end if.
END LOOP.
exe.

See the VECTOR command in SPSS. You can not just create loop and treat variables as in array. They must first be put into vectors. Also, check the COMPUTE command. I think SUM would be more appropriate because if you write " compute v1 = v2 + v3 " and v2 has data but v3 is blank, v1 will be blank.



来源:https://stackoverflow.com/questions/12711613/loop-through-items-and-sum-items-in-spss

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