Date: Thu, 15 Jul 2004 10:30:41 -0400
Reply-To: Howard Schreier <Howard_Schreier@ITA.DOC.GOV>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Howard Schreier <Howard_Schreier@ITA.DOC.GOV>
Subject: Re: Compare Dates
It ends up involving a fair amount of code, but that's due to logic
requirements. It's not "wallpaper" (a repeating pattern).
Your test data:
data shortexample;
input id date: mmddyy8.;
format date date9.;
cards;
1 01-05-96
1 01-05-96
1 01-05-96
1 01-08-96
1 01-08-96
1 01-10-96
2 06-01-97
2 06-08-97
2 06-09-97
2 06-10-97
;
Code:
data admissions(drop=date);
retain admit discharge;
format admit discharge date9.;
set shortexample(where = (not missing(date) ) );
by id;
* Seek admission and discharge dates;
if missing(admit) then admit = date;
else if missing(discharge) then do;
if date<admit then admit = date;
* in which case dates are out of order!;
else if date>admit then discharge = date;
* else (ie, date=admit) ignore repetition of admit date
(so no admit and discharge on same day);
end;
* Seek end of episode;
if n(admit,discharge)=2 or last.id then do;
if date>discharge or last.id then do;
* end of episode, so output and reset;
if last.id then discharge = date;
output;
if last.id then admit = . ;
else admit = date;
discharge = .;
end;
else do;
* not end of episode;
if date<admit then do;
* dates are out of order!;
admit = date;
discharge = .;
end;
* else date is in known interval and simply ignored;
end;
end;
run;
Result:
Obs admit discharge id
1 05JAN1996 10JAN1996 1
2 01JUN1997 08JUN1997 2
3 09JUN1997 10JUN1997 2
What is needed now is a much more thorough test to verify correct behavior
for on-boundary conditions, exceptions, special cases, and the like.
On Mon, 12 Jul 2004 03:03:45 -0400, L.Jones <laura-jones@UIOWA.EDU> wrote:
>I have a list of dates that I need to compare and replace as necessary to
>determine admission and discharge dates.
>
>Here's a short example of dates listed:
>
>ID DATE
>1 01-05-96
>1 01-05-96
>1 01-05-96
>1 01-08-96
>1 01-08-96
>1 01-10-96
>2 06-01-97
>2 06-08-97
>2 06-09-97
>2 06-10-97
>
>By default the very first date is the admission date. As an initial
>default discharge date, it can be set to the 2nd date listed.
>I then want to compare the 3rd date to the 2nd date. If it is the same
>date or before then the first admission and discharge dates (created
>initially) it should be retained. If the 3rd date occurs after the 2nd
>date, then I want to create a 2nd admission date and by default set the
>2nd discharge date to the 4th date listed. This continues for each id
>number until the end.
>
>What I'd like to see is something like this:
>
>ID ADMISSION1 DISCHARGE1 ADMISSION2 DISCHARGE2
>1 01-05-96 01-10-96 . .
>2 06-01-97 06-08-97 06-09-97 06-10-97
>
>How should I go about doing this? I could easily write this with several
>lines of code, but the problem is that some id numbers have hundreds of
>dates listed and I don't want to have to write hundreds of lines of code.
>
>I'm guessing that I probably need some type of array statement with some
>do loops and with a counter.
>
>I initially thought that I should change the format of the data and use an
>array to create one line of code per ID number, but then I got stuck
>trying to figure out how to create new admission and discharge dates
>without the tedious and multiple lines of code.
>
>Any help or ideas would be appreciated.
>
>Thanks,
>L.Jones
|