BookmarkSubscribeRSS Feed
Jest
Obsidian | Level 7

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

 

2 REPLIES 2
art297
Opal | Level 21

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

 

mkeintz
PROC Star

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.

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 714 views
  • 0 likes
  • 3 in conversation