'SPSS Sum the data that meets particular condition (numerical variable)
I am trying to sum the income of the particular members that shares the same "index" number and present it as a new variable HHincome.
For example: Members with index value of 1 would be summed and presented as a new variable HHincome = 26+31+9+7.
The index number exist from 1 to 466 and there are total of 2504 members.
The followings are the commands that i tried to implement but it doesn't seem to work.
compute HHincome = 0.
execute.
vector vindex = index1 TO index466.
vector vincome = income1 TO income2504.
LOOP #vecid = 1 TO 466.
do IF (vindex(#vecid) eq 1 and not missing (vincome(#vecid))).
COMPUTE HHincome = HHincome+vincome(#vecid).
end if.
End LOOP.
EXECUTE.
This is my other attempt
LOOP j = 1 to 466.
compute HHincome = 0.
Loop i = 1 to 2504.
do if (data[i, index] eq j).
compute HHincome = HHincome + data[i,income].
end if.
End loop.
End loop.
Execute.
Solution 1:[1]
In SPSS you don't need to loop over rows (vector and loop are for looping over columns). What you need instead is the aggregate function. Like this:
aggregate outfile=* mode=addvariables /break=index /HHincome=sum(income).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | eli-k |
