BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
park2039
Calcite | Level 5

I am building a longitudial data for my study.  The input data has id, year, event, and event year variables.  I am trying to create a variable z which is time elapsed from the last event.  Can someone show a data step that computes z?  Thanks.

id   year     event    event_yr       z

1    1986      0          .

1    1987      0          .

1    1988      0          .

1    1989      0          .

1    1990      0          .

1    1991      0          .

1    1992      0          .

1    1993      0          .

1    1994      0          .

1    1995      0          .

1    1996      1       1996        

1    1997      0          .

1    1998      0          .

1    1999      0          .

1    2000      0          .

1    2001      1       2001              5      

1    2002      0          .

1    2003      0          .

1    2004      0          .

1    2005      0          .

1    2006      0          .

1    2007      0          .

1    2008      1       2008              7

1    2009      0          .

1    2010      0          .

1    2011      0          .

1    2012      0          .

1    2012      0          .

2    2010      0          .

2    2011      1       2011              0

2    2012      0          .

3    2007      0          .

3    2008      1       2008              0

3    2009      1       2009              1

3    2010      0          .

3    2011      0          .

3    2012      1       2012              3

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

: Assuming your data are already sorted like in your example, and that you really want to assign a 0 for id eq 1 and event_yr eq 1996, then something like the following should suffice:

data want;

  set have;

  by id;

  retain last_event;

  if first.id then call missing(last_event);

  if event eq 1 then do;

    z=ifn(last_event gt 0,event_yr-last_event, 0);

    last_event=event_yr;

  end;

run;

View solution in original post

1 REPLY 1
art297
Opal | Level 21

: Assuming your data are already sorted like in your example, and that you really want to assign a 0 for id eq 1 and event_yr eq 1996, then something like the following should suffice:

data want;

  set have;

  by id;

  retain last_event;

  if first.id then call missing(last_event);

  if event eq 1 then do;

    z=ifn(last_event gt 0,event_yr-last_event, 0);

    last_event=event_yr;

  end;

run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 1 reply
  • 304 views
  • 0 likes
  • 2 in conversation