I have a longitudinal dataset with time variables (months) and event (0,1)...
How can I create a data table showing the time as when the event occurred and if the event did not occur the last follow-up time.
This is how the data looks.
ID Time Event
1 0 0
1 1 0
1 2 1
1 3 1
1 4 1
2 0 0
2 1 0
2 2 0
2 3 0
2 4 0
I want the data to look like:
ID time event
1 2 1
2 4 0
Thanks
Here is one way:
data want (drop=_:);
set have;
retain _need;
by id;
if first.id then _need=1;
if _need and (event eq 1 or last.id) then do;
output;
_need=0;
end;
run;
HTH,
Art, CEO, AnalystFinder.com
Another way:
data want;
set have;
by id event;
if (first.event=1 and event=1) or (last.id=1 and event=0);
run;
This assumes that once a record with event=1 occurs, then event stays at 1 for the remaining records of the id. And it assumes that data are sorted by ID.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.