Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Grouping Cases on SPSS by Date Criteria

80 views
Skip to first unread message

camer...@hotmail.co.uk

unread,
May 16, 2016, 8:32:38 AM5/16/16
to
Hi,

I am trying to come up with some syntax for the following query but have so far been unsuccessful.

Example Table:

Person DateSpec Primary Record
Person1 24/05/2015 1
Person1 02/06/2016 0
Person1 02/06/2015 0
Person1 01/01/2016 0
Person1 04/02/2016 0
Person2 12/09/2015 1
Person2 21/10/2015 0
Person2 12/03/2016 0
Person2 20/03/2016 0



I have grouped all the cases by various identifiers so that they are now grouped togethPerson1er. the 'PrimaryRecord' variable is used to separate each person.
The next task I would like to do is further separate these individuals by 'DateSpec'. This is where I have run into difficulty. I would want to group it so that all dates for one individual that fall within 6 weeks (42 days) of the current PrimaryRecord are grouped together. For example, Person1 would have two different groups, first three rows would be grouped together and then the following two rows would be classed as separate.

Ideally I would like this to follow the same primary record pattern as above so that person1 would then have two 1s next to them. Or if anyone out there has any sort of way of generating an unique ID to group these together that would be even better.

Any help or advice on this would be greatly appreciated, even if its to say it cant be done.

Thanks in advance and feel free to fire questions at me.

Bruce Weaver

unread,
May 16, 2016, 1:43:17 PM5/16/16
to
Does this do what you want?

DATA LIST list / Person (F5.0) DateSpec (DATE) PrimaryRecord(F1).
BEGIN DATA
1 24/05/2015 1
1 02/06/2016 0
1 02/06/2015 0
1 01/01/2016 0
1 04/02/2016 0
2 12/09/2015 1
2 21/10/2015 0
2 12/03/2016 0
2 20/03/2016 0
END DATA.

* Store Primary Date for each ID in a scratch variable.
NUMERIC #PrimaryDate(DATE11).
DO IF PrimaryRecord.
- COMPUTE #PrimaryDate = DateSpec.
ELSE.
- COMPUTE #PrimaryDate = LAG(#PrimaryDate).
END IF.
* Compute the difference (in months) from the primary date.
COMPUTE DiffMonths = DATEDIFF(DateSpec,#PrimaryDate,"months").
* Flag cases where the difference is 6 months or less.
COMPUTE Within6Mos = DATEDIFF(DateSpec,#PrimaryDate,"months") LE 6.
FORMATS DiffMonths (F5.0) / Within6Mos (F1).
LIST.

OUTPUT:

Person DateSpec PrimaryRecord DiffMonths Within6Mos

1 24-MAY-2015 1 0 1
1 02-JUN-2016 0 12 0
1 02-JUN-2015 0 0 1
1 01-JAN-2016 0 7 0
1 04-FEB-2016 0 8 0
2 12-SEP-2015 1 0 1
2 21-OCT-2015 0 1 1
2 12-MAR-2016 0 6 1
2 20-MAR-2016 0 6 1

Rich Ulrich

unread,
May 16, 2016, 11:11:43 PM5/16/16
to
On Mon, 16 May 2016 10:43:15 -0700 (PDT), Bruce Weaver
<bwe...@lakeheadu.ca> wrote:

I'd do the 6 weeks as 42 days instead of 6 months.
Then I would make Primary Record into a counter.

--
Rich Ulrich

camer...@hotmail.co.uk

unread,
May 17, 2016, 4:32:38 AM5/17/16
to
Hi Guys,

Thanks for the responses!

I have edited the above syntax to compute 42 days instead of 6 months and left with:

* Store Primary Date for each ID in a scratch variable.
NUMERIC #PrimaryDate(DATE11).
DO IF PrimaryFirst.
- COMPUTE #PrimaryDate = SpecimenDate.
ELSE.
- COMPUTE #PrimaryDate = LAG(#PrimaryDate).
END IF.
* Compute the difference (in months) from the primary date.
COMPUTE DiffDays = DATEDIFF(SpecimenDate,#PrimaryDate,"days").
* Flag cases where the difference is 6 months or less.
COMPUTE Within42Days = DATEDIFF(SpecimenDate,#PrimaryDate,"days") LE 42.
FORMATS DiffDays (F5.0) / Within42Days (F1).
LIST.

Calculating the difference in days between the primary record and the other associated records works really well. Im having a problem with the 'Within42Days' column as it has a 1 if the record is within 42 days and a zero when it is over 42 days. Ideally I would like a 1 on the primary record and then a 1 on any other record out with the 42 days. With zeros on the records that fall within the 42 days. Any help would be greatly appreciated

Bruce Weaver

unread,
May 17, 2016, 2:25:15 PM5/17/16
to
Is this what you want?

COMPUTE Beyond42Days = DATEDIFF(SpecimenDate,#PrimaryDate,"days") GT 42.

camer...@hotmail.co.uk

unread,
May 18, 2016, 3:40:48 AM5/18/16
to
Thanks Bruce! That solves that problem fantastically. The only other issue I have which I cant seem to get my head around is that I would ideally like to create a new PrimaryRecord column that had the existing number 1s from the current column plus a number 1 next to the first date within a grouped person that exceeds 42 days. With the rest zeros. Example:

Person DateSpec PrimaryRecord DiffDays Beyond42Days PrimaryRecord2(NEW FIELD)

1 24-MAY-2015 1 0 0 1
1 02-JUN-2016 0 9 0 0
1 02-JUN-2015 0 0 0 0
1 01-JAN-2016 0 222 1 1
1 04-FEB-2016 0 256 1 0
2 12-SEP-2015 1 0 0 1
2 21-SEP-2015 0 9 0 0
2 12-MAR-2016 0 360 1 1
2 20-MAR-2016 0 368 1 0

Is this possible to do? Or maybe its a step to far.

Thanks again

camer...@hotmail.co.uk

unread,
May 18, 2016, 4:50:14 AM5/18/16
to
Just to further clarify, as I don't think I have very well. The 42 days is an episode and an individual can have multiple episodes. Each episode should have a 1 in the PrimaryRecord2 column next to the earliest date of that episode

Bruce Weaver

unread,
May 18, 2016, 10:36:59 AM5/18/16
to
On Wednesday, May 18, 2016 at 4:50:14 AM UTC-4, camer...@hotmail.co.uk wrote:
> Just to further clarify, as I don't think I have very well. The 42 days is an episode and an individual can have multiple episodes. Each episode should have a 1 in the PrimaryRecord2 column next to the earliest date of that episode

Does "multiple episodes" mean more than 2? I.e., does each block of 42 days constitute an episode? Or is episode 1 anything within the first 42 days, and episode 2 anything after that?

Rich Ulrich

unread,
May 19, 2016, 2:35:03 PM5/19/16
to
I suggested a few days ago that there should be a count
of episodes.

"What do I desperately need next?" -- is a horrible way
to proceed, especially when asking questions of experts.

"What pointers/counters/indicators might I ever want to use?"
Episode number; counter within episode; extra marker for "last"
within an episode.

In these data, it is possible that the OP has never adequately
considered WHAT he wants to define for an "episode". - I
remember that "computerization" required hospital administrators
to focus for the first time on some explicit definitions ... if a
patient goes AWOL for an hour, or a few hours, or longer,
what determines (for the records) whether there has to be
a re-admission? How long was there "an empty bed"? Do
you "close out" a patient-record based on the time elapsed?
by 24-hour clock? by nurses' shifts?

How does "42 days" figure into the useful definition of an episode?
Clinically or pragmatically: Is it really a "new episode" if
treatment has been continuous? or should there be a definition
which includes a necessary gap?)

--
Rich Ulrich
0 new messages