Keeping observations by longidutinal data techniques

Solved
Regular Contributor
Posts: 150

Keeping observations by longidutinal data techniques

[ Edited ]

I have a longitudinal dataset (assuming it is already sorted by increasing time):

ID   DX

1     0

2     1

2     0

3     2

4     0

4     1

4     2

5     0

I want to keep the observations in the dataset where DX is 1 or 2. But, if DX is 0, I only want to throw it out if (1) it is the only record for that ID or (2) if it is the first observation for that ID and is followed by a DX of 1 or 2.For the example above, it would look like:

ID   DX

2     1

2     0

3     2

4     1

4     2

How can I do this? Thank you!

Accepted Solutions
Solution
‎12-15-2017 01:10 AM
Posts: 1,309

Re: Keeping observations by longidutinal data techniques

It should be straightforward:

``````data have;
input ID   DX  @@;
datalines;
1 0   2 1   2 0   3 2
4 0   4 1   4 2   5 0
run;

data want ;
set have (keep=id);
by id ;
merge have have (firstobs=2 keep=dx rename=(dx=nxt_dx));
if first.id=1 and dx=0 and (last.id=1 or nxt_dx in (1,2)) then delete;
run;``````

`` ``

The if test asks whether the id starts with 0, ("first.id=1 and dx=0"),   and whether that zero is a single record ("and last.id=1") or is followed by a 1 or 2 ("nxt_dx in (1,2)").

All Replies
Contributor
Posts: 35

Re: Keeping observations by longidutinal data techniques

Assuming that an ID can't have two instances of DX=0, you could try:

data work.want;
set work.have;
by ID;
if DX eq 0 then do;
if (first.ID and last.ID) then delete;
else if (first.ID and not last.ID) then delete;
end;
run;

Solution
‎12-15-2017 01:10 AM
Posts: 1,309

Re: Keeping observations by longidutinal data techniques

It should be straightforward:

``````data have;
input ID   DX  @@;
datalines;
1 0   2 1   2 0   3 2
4 0   4 1   4 2   5 0
run;

data want ;
set have (keep=id);
by id ;
merge have have (firstobs=2 keep=dx rename=(dx=nxt_dx));
if first.id=1 and dx=0 and (last.id=1 or nxt_dx in (1,2)) then delete;
run;``````

`` ``

The if test asks whether the id starts with 0, ("first.id=1 and dx=0"),   and whether that zero is a single record ("and last.id=1") or is followed by a 1 or 2 ("nxt_dx in (1,2)").

PROC Star
Posts: 1,558

Re: Keeping observations by longidutinal data techniques

Hi, Your sample makes me wonder whether a "Look Ahead" is even required once you determine the value of first.id is 0? Perhaps my bad, i should assume the look ahead obs could have any other number besides 0,1,2.

Regular Contributor
Posts: 150

Re: Keeping observations by longidutinal data techniques

Posted in reply to novinosrin
I think you are right, I only care about observations that start with my diagnosis of interest, I suppose in the case of a 0 then a 0 then a 1, I would then throw out the first 2 observations though.
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
• 4 replies
• 146 views
• 1 like
• 4 in conversation