SPSS - 在一个日历月内标记案例

时间:2017-04-18 15:24:34

标签: spss

我有一份囚犯名单,他们的监禁期开始时(PrisonStart)和结束时(监狱结束)。如果他们还在狱中,监狱结局是空白的。

我想在六个月内(2016年1月1日至2016年5月30日)标记在监狱中至少有一个完整日历月的囚犯。

compute PeriodBeg = date.mdy(01,01,16).
compute PeriodEnd = date.mdy(05,30,16).
formats PeriodBeg PeriodEnd (adate10).
execute.

有关如何最好地解决此问题的任何建议?似乎我可能需要在6个月期间(如下所示)分别比较每个月的囚犯的开始和结束日期,然后选择任何至少有一个月的囚犯,但我想知道是否有更有效的方式。

if ((PrisonStart le [January 1, 2016]) and (PrisonEnd ge ([January 31, 2016]) | missing(PrisonEnd))) InPrisonJan = 1.
if ((PrisonStart le [February 1, 2016]) and (PrisonEnd ge ([February 28, 2016]) | missing(PrisonEnd))) InPrisonFeb = 1.
etc.
execute.

以下一些示例数据。前两名囚犯应被标记为在6个月期间(OneMonth = 1)入狱至少一整个日历月。在6个月期间(OneMonth = 0),最后三名囚犯在一个完整日历月内没有入狱。

data list list /PrisonerID (F8.0) PrisonStart (adate10) PrisonEnd (adate10)    PeriodBeg (adate10) PeriodEnd (adate10).
begin data
1 10.3.14 7.12.16 1.1.16 5.30.16
2 2.9.16 4.1.16 1.1.16 5.30.16
3 5.2.16 10.11.16 1.1.16 5.30.16
4 12.1.13 2.8.14 1.1.16 5.30.16 
5 1.7.16 1.20.16 1.1.16 5.30.16
6 1.1.17 3.2.17 1.1.16 5.30.16
end data.

1 个答案:

答案 0 :(得分:1)

以下语法避免单独提及每个月的最后一天,因此可以在任意数月内自动执行。检查日期是否是该月的最后一天的诀窍是检查日期" 0"下个月:

do repeat inpr=inPrison1 to inPrison5/ mon=1 to 5.
   compute inpr=( PrisonStart<=date.dmy(1,mon,2016) and 
                  (PrisonEnd>=date.dmy(0,mon+1,2016) or missing (PrisonEnd)) ).
end repeat.