SPSS 对满足特定条件的数据求和(数值变量)
SPSS Sum the data that meets particular condition (numerical variable)
我正在尝试对共享相同“索引”编号的特定成员的收入求和,并将其作为新变量 HHincome 呈现。
例如:
Members with index value of 1 would be summed and presented as a new variable HHincome = 26+31+9+7.
索引号从1到466共有2504个成员
以下是我尝试执行但似乎不起作用的命令。
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.
这是我的另一次尝试
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.
在 SPSS 中,您不需要遍历行(vector
和 loop
用于遍历列)。您需要的是 aggregate
函数。像这样:
aggregate outfile=* mode=addvariables /break=index /HHincome=sum(income).
我正在尝试对共享相同“索引”编号的特定成员的收入求和,并将其作为新变量 HHincome 呈现。
例如: Members with index value of 1 would be summed and presented as a new variable HHincome = 26+31+9+7.
索引号从1到466共有2504个成员
以下是我尝试执行但似乎不起作用的命令。
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.
这是我的另一次尝试
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.
在 SPSS 中,您不需要遍历行(vector
和 loop
用于遍历列)。您需要的是 aggregate
函数。像这样:
aggregate outfile=* mode=addvariables /break=index /HHincome=sum(income).