Login  Register

Re: Counting Consecutive Dates or Gaps in dates

Posted by Richard Ristow on Apr 04, 2007; 7:10pm
URL: http://spssx-discussion.165.s1.nabble.com/Counting-Consecutive-Dates-or-Gaps-in-dates-tp1074911p1074912.html

At 10:17 AM 4/4/2007, Hughes, John wrote:

>I have one record per client per month but some have stops and
>restarts. I would like to count either how many gaps there are in
>service or the number of months of service that are consecutive.

Might as well count both. This is a pure 'long' solution, i.e. keeping
months in separate cases; there is, therefore, no particular
restriction on the length of history for one case. This is SPSS 15
draft output (WRR-not saved separately):

|-----------------------------|---------------------------|
|Output Created               |04-APR-2007 13:56:52       |
|-----------------------------|---------------------------|
  id month

001   01
001   02
001   03
001   04
001   05
002   01
002   03
002   04
003   02
003   03
003   04
003   05
004   01
004   02
004   04
004   05

Number of cases read:  16    Number of cases listed:  16


NUMERIC  GAP   (F3)
         /CONSEC(F4).
VAR LABEL
     GAP    'Month immediately follows a gap in service'
     CONSEC 'Months of consecutive svc, thru this one'.

DO IF    MISSING(LAG(ID))
       OR         LAG(ID) NE ID.
.  COMPUTE  GAP    = 0.
.  COMPUTE  CONSEC = 1.
ELSE IF  MONTH EQ LAG(MONTH)     + 1.
.  COMPUTE  GAP    = 0.
.  COMPUTE  CONSEC = LAG(CONSEC) + 1.
ELSE.
.  COMPUTE  GAP    = 1.
.  COMPUTE  CONSEC = 1.
END IF.
LIST.

List
|-----------------------------|---------------------------|
|Output Created               |04-APR-2007 13:56:53       |
|-----------------------------|---------------------------|
  id month GAP CONSEC

001   01    0     1
001   02    0     2
001   03    0     3
001   04    0     4
001   05    0     5
002   01    0     1
002   03    1     1
002   04    0     2
003   02    0     1
003   03    0     2
003   04    0     3
003   05    0     4
004   01    0     1
004   02    0     2
004   04    1     1
004   05    0     2

Number of cases read:  16    Number of cases listed:  16


AGGREGATE OUTFILE=*
    /BREAK = ID
    /GAPS   'Number of gaps in svc' = SUM(GAP)
    /CONSEC 'Longest consecutive period of service' = MAX(CONSEC).
FORMATS GAPS  CONSEC (F3).
LIST.

List
|-----------------------------|---------------------------|
|Output Created               |04-APR-2007 13:56:53       |
|-----------------------------|---------------------------|
  id GAPS CONSEC

001    0     5
002    1     2
003    0     4
004    1     2

Number of cases read:  4    Number of cases listed:  4
======================================
APPENDIX: Test data (Mahbub Khandoker)
======================================
DATA LIST free/ id(n2) month(n2).
Begin data
1 1
1 2
1 3
1 4
1 5
2 1
2 3
2 4
3 2
3 3
3 4
3 5
4 1
4 2
4 4
4 5
End data.
FORMATS ID    (N3)
        /MONTH (N2).
SORT CASES BY ID MONTH.
LIST.