BookmarkSubscribeRSS Feed
Fletcher
Calcite | Level 5

Hi All,

New SAS user here.  I have a panel data structure, from which I am trying to calculate the duration between observations.  My test data looks like this:

ID     Enter Date     Exit Date     Sex     Age

1        1/1/2000       1/1/2002       m       24

1        5/5/2006        5/5/2009      m       30

1        8/8/2011        8/8/2013      m       35

2        2/2/2002        2/2/2005       f        42

2        6/6/2006        6/6/2010       f        46

3        3/3/2003        3/3/2004      m       19

3        4/4/2008        4/4/2012      m       24

I want to create a variable that subtracts the exit date from the next enter date, so that last observation for each ID group is missing.  Any efficient ways to do this without running loop?

Thanks in Advance.

3 REPLIES 3
PGStats
Opal | Level 21

Don't know what you mean by "without running loop", but this is efficient :

data test;
informat enterDate exitDate mmddyy9.;
format enterDate exitDate mmddyy10.;
input ID     EnterDate     ExitDate     Sex $    Age;
datalines;
1        1/1/2000       1/1/2002       m       24
1        5/5/2006        5/5/2009      m       30
1        8/8/2011        8/8/2013      m       35
2        2/2/2002        2/2/2005       f        42
2        6/6/2006        6/6/2010       f        46
3        3/3/2003        3/3/2004      m       19
3        4/4/2008        4/4/2012      m       24
;

data want;
set test end=end;
if not end then do;
      set test (firstobs=2 keep=id enterDate rename=(enterDate=nextDate id=nextId));
      if id=nextId then duration = intck("DAY", exitDate, nextDate);
      end;
run;


proc print; run;

PG

PG
Linlin
Lapis Lazuli | Level 10

data want;

  merge test(in=a) test(firstobs=2 keep=id enterdate rename=(id=_id enterdate=_enterdate));

   duration=ifn(id=_id,intck("DAY",exitdate, _enterdate),.);

   if a;

   drop _:;

  proc print;run;

Haikuo
Onyx | Level 15

Here is an obnoxious SQL approach with the undocumented feature:

data have;

input ID     (Enter_Date     Exit_Date) (:mmddyy10.)   Sex$     Age;

format     Enter_Date     Exit_Date mmddyy10.;

cards;

1        1/1/2000       1/1/2002       m       24

1        5/5/2006        5/5/2009      m       30

1        8/8/2011        8/8/2013      m       35

2        2/2/2002        2/2/2005       f        42

2        6/6/2006        6/6/2010       f        46

3        3/3/2003        3/3/2004      m       19

3        4/4/2008        4/4/2012      m       24

;

proc sql;

select a.*,b.enter_date-a.exit_date as new_date from

(select *, monotonic() as obs from have) a

left join

(select *, monotonic() as obs from have) b

on a.id=b.id and

b.obs-a.obs=1

order by id, enter_date;

quit;

Haikuo

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
  • 3 replies
  • 848 views
  • 0 likes
  • 4 in conversation