Date: Thu, 19 Mar 2009 12:04:43 -0700 "Terjeson, Mark" "SAS(r) Discussion" "Terjeson, Mark" Re: Calculate time interval To: Sid A<200903191841.n2JGa3pp023800@malibu.cc.uga.edu> text/plain; charset="US-ASCII"

Hi Sid,

Here is one approach using BY processing which set FIRST. and LAST. flags to use:

data sample; input subjid visit visitdt date9. status \$; format visitdt date9.; cards; 1 1 31dec2008 CR 1 2 14jan2009 PR 1 3 22jan2009 SD 1 4 07feb2009 PD ; run;

* insure BY ordering ; proc sort data=sample; BY subjid visit; run;

* use FIRST. processsing ; data result(drop=prev_date start_date); set sample; BY subjid visit; prev_date = lag(visitdt); * date from prior record ;

* reset new var for each subj ; if first.subjid then start_date = .;

* save PR date if found ; if status eq 'PR' then start_date = visitdt;

* use PR date if found, else use last date ; if start_date eq . then do; if not first.subjid then start_date = prev_date; if last.subjid then diff = visitdt - start_date; end; run;

Mark Terjeson Senior Programmer Analyst Investment Management & Research Russell Investments 253-439-2367

Russell Global Leaders in Multi-Manager Investing

-----Original Message----- From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of Sid Sent: Thursday, March 19, 2009 11:42 AM To: SAS-L@LISTSERV.UGA.EDU Subject: Calculate time interval

I have a dataset like below:

subjid visit visitdt status 1 1 -- CR 1 2 --- PR 1 3 --- SD 1 4 --- PD

I need to calculate the difference in time - from when status is PR to PD. PD is always the last visit, PR can occur in any of the visits. If PD is not present I need to replace PD with the last visit's date.

Thanks, Sid

Back to: Top of message | Previous page | Main SAS-L page