每行中有多个比较

时间:2019-05-05 09:44:16

标签: spss

在SPSS上,我有一个包含6000多人的电子表格。每个人至少参加了两次测试,并且同一测试至少获得了两个结果。有些人参加了两次以上的考试。 SPSS是否可以查看两个测试是否在6个月内,并且是否将这些测试的结果包括在人员旁边并删除所有其他结果?

数据结构

PersonNo  Test Date  Test Result  Test Date 2  Test result 2, Test Date 3, Test result 3
PersonNo 512, 23-Aug-18, 65, 22-May-18, 72

问题

PersonNo 98432, 09-Feb-18, 74, 06-Nov-18, 76, 10-Aug-18, 67
PersonNo 91203, 10-Dec-18, 75, 10-Sep-18, 65
PersonNo 75432, 01-Jan-18, 65, 01-Dec-18, 65

我想要的方式

PersonNo 98432, 09-Feb-18, 74, 10-Aug-18, 67
PersonNo 91203, 10-Dec-18, 75, 10-Sep-18, 65

编号75432的人员被删除,因为他们在6个月内没有两次测试结果

2 个答案:

答案 0 :(得分:1)

要查看在TestDate变量的6个月内进行了哪些测试,可以使用DATEDIFF(date1, date2, units)。由于尚不清楚您有多少TestDate字段,因此您可能需要对变量重新排序,以使用VECTOR命令在它们之间循环。

* assumes there are up to 11 tests each respondent may have taken.
VECTOR nextTestDate = TestDate2 TO TestDate 11 .
VECTOR nextTestResult = TestResult2 TO TestResult11 .
LOOP #i = 1 TO 10 .
* if not within 6 months then set date & result to sysmis .
DO IF (DATEDIFF(TestDate, nextTestDate(#i), 'days') > 182) .
  RECODE nextTestDate(#i) nextTestResult(#i) (ELSE = SYSMIS) .
END IF .
END LOOP .
EXE .

如果您只需要检查几个TestDate字段,则无需在VECTOR内执行此操作。在这里,您可以删除其中不再包含任何数据的任何变量(可以使用DESC TestResult2 TO TestResult11轻松检查)。

答案 1 :(得分:1)

我建议使用一个重组结构,而不是双重循环和多重比较,它只能对连续的测试进行排序和比较。

首先,我要创建一个小的假数据来演示:

data list free/PersonNo (f6)   date1(Date11) score1(f3)    Date2(date11) score2 (f3)     Date3 (date11) score3(f3)   Date4 (date11) score4(f3).
begin data
98432, 09-Feb-18, 74, 06-Nov-18, 76, 10-Aug-18, 67, ,
91203, 10-Dec-18, 75, 10-Sep-18, 65, , , ,
75432, 01-Jan-18, 65, 01-Dec-18, 65, , , ,
12345, 19-Mar-18, 74, 26-Dec-19, 55, 10-Aug-18, 81, 19-Feb-19, 77
end data.

现在执行实际任务:

* first step - restructuring to long format.

varstocases /make date from date1 date2 date3 date4/make score from score1 score2 score3 score4.

* now it is possible to sort by test date, compare the dates and keep only the relevant ones.

sort cases by PersonNo date.
if $casenum>1 and PersonNo=lag(PersonNo) cond=DATEDIFF(date, lag(date), 'days') < 182.
create cond2=lead(cond,1).
select if cond or cond2.
exe.

* At this point you have only the relevant persons and tests left. 
You might continue your analysis in this structure, 
but if you want the following code gets you back to the original structure.

compute ind=1.
if $casenum>1 and PersonNo=lag(PersonNo) ind=lag(ind)+1.
format ind(f1).
casestovars /id=PersonNo /index=ind /drop cond cond2 /groupby=index /separator="".