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!
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)").
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;
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)").
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.