问题
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