在不同的行中查找值

时间:2016-02-17 17:10:17

标签: spss

我很难解决以下问题。

我的数据集看起来像这样:

ID Personal number Place of birth Mother in house  Personal number mother  v6
2         1           FL                 1                     2 
2         3           NY                 0                     0
2         4           FL                 0                     2
2         2           CA                 0                     0
4
4

我想要创建的是一种语法,说明如果母亲在家里' = 1,请查看母亲的个人号码'与匹配的身份证号码,并在v6给我母亲的出生国。 enter image description here

1 个答案:

答案 0 :(得分:0)

基本上这可以通过创建具有“ID”“个人号码”和“出生地”的查找表来完成。在第二步中,将查找数据与母亲数据进行匹配。

以下代码将执行此操作(您必须调整变量名称):

DATASET NAME orig.

*** Create lookuptable with "ID" and "Personal Number" as unique matching key
*** and "Place of Birth" as matching value.
* (Every combination of "ID" and "Personal Number" have to be unique in 
* the original data set in order to make this procedure work properly)
DATASET COPY lookup.
DATASET ACTIVATE lookup.

* Just keep the variables "ID", "Personal Number" and "Place Of birth" in the lookup table.
MATCH FILES
/FILE *
/KEEP ID PersNr PlaceOfBirth.

* Sort data set according to matching key.
SORT CASES BY ID PersNr.

*** Match lookup table with original table.
*** Use "ID" and "Personal Number Mother" as matching key.
DATASET ACTIVATE orig.

* Sort data set according to matching key.
SORT CASES BY ID PersNrOfMother.

* Match persNrOfMother with data from the lookup table.
MATCH FILES 
/FILE *
/TABLE lookup
    /RENAME (PersNr = PersNrOfMother) (PlaceOfBirth = PlaceOfBirthMother)
/BY ID PersNrOfMother.

* Delete PlaceOfBirthMother value if mother is not in house.
IF (MotherInHouse=0) PlaceOfBirthMother = "".
EXECUTE.

请记住,这将更改数据的大小写顺序,因为您必须为匹配过程求助数据。如果要恢复原始订单,首先必须创建一个存储原始订单的变量(例如使用命令COMPUTER casenumber = $casenum.),然后使用此变量最终求助SORT CASES BY casenumber.