对于一个学校项目,我发现自己正在使用人口普查局当前人口调查的数据。我选择了SPSS来处理数据,因为在我有限的时间范围内,它似乎是最简单的软件。一切看起来都很简单,除了一个给我带来麻烦的操作。
对于我的数据集中的每个案例 - 每个案例代表一个被调查的个人 - 定义了以下(相关)变量:
问题在于:我需要将任何给定父级的KIDHASHEALTH值设置为HHID和POPNUM或MOMNUM值与当前案例的HHID和PID匹配的最年轻人的HASHEALTH值 - 功能上是他们最小的孩子。
到目前为止,我一直无法弄清楚如何使用SPSS语法来做到这一点。任何人都可以想办法用语法或其他方法完成我想要做的事情吗?
很多,非常感谢提前。
使用示例数据进行编辑:
HHID |PID |AGE |POPNUM |MOMNUM |HASHEALTH |KIDHASHEALTH
-----+----+----+-------+-------+----------+------------
1 |1 |45 |0 |0 |0 |0 //KIDHASHEALTH == 0 because
1 |2 |48 |0 |0 |0 |0 //youngest child's HASHEALTH == 0
1 |3 |13 |1 |2 |0 |0
2 |1 |33 |0 |0 |0 |1 // == 1 because youngest child's
2 |2 |28 |0 |0 |0 |1 // HASHEALTH == 1
2 |3 |15 |1 |2 |0 |0
2 |4 |12 |1 |2 |1 |0
-----+----+----+-------+-------+----------+------------
答案 0 :(得分:2)
以下代码仅在您的小型数据代码中进行了测试。因此,不保证所有数据具有其特殊性。该代码假设AGE是整数。
*Let's add small fractional noise to those children AGE who HASHEALTH=1.
*In order to insert the info about health right into the age number.
if hashealth age= age+rv.unif(-.1,+.1).
*Turn to fathers. Combine POPNUM and PID numbers in one column.
compute parent= popnum. /*Copy POPNUM as a new var PARENT.
if parent=0 parent= pid. /*and if the case is not a child, fill there PID.
*Now a father and his children have the same code in PARENT
*and so we can propagate the minimal age in that group (which is the age of the
*youngest child, provided the man has children) to all cases of the group,
*including the father.
aggregate /outfile= * mode= addvari
/break= hhid parent /*breaking is done also by household, of course
/youngage1= min(age). /*The variable showing that minimal age.
*Turn to mothers and do the same thing.
compute parent= momnum.
if parent=0 parent= pid.
aggregate /outfile= * mode= addvari
/break= hhid parent
/youngage2= min(age). /*The variable showing that minimal age.
*Take the minimal value from the two passes.
compute youngage= min(youngage1,youngage2).
*Compute binary KIDHASHEALTH variable.
*Remember that YOUNGAGE is not integer if that child has HASHEALTH=1.
compute kidhashealth= 0.
if popnum=0 and momnum=0 /*if we deal with a parent
and age<>youngage /*and the youngage age listed is not their own
and rnd(youngage)<>youngage kidhashealth= 1. /*and the age isn't integer, assign 1.
compute age= rnd(age). /*Restore integer age
exec.
delete vari parent youngage1 youngage2 youngage.