LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous (more recent) messageNext (less recent) messagePrevious (more recent) in topicNext (less recent) in topicPrevious (more recent) by same authorNext (less recent) by same authorPrevious page (April 1998, week 1)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:   Fri, 3 Apr 1998 10:15:18 -0800
Reply-To:   "William W. Viergever" <wwvierg@IBM.NET>
Sender:   "SAS(r) Discussion" <SAS-L@UGA.CC.UGA.EDU>
From:   "William W. Viergever" <wwvierg@IBM.NET>
Subject:   Re: Difficult problem with dates
Comments:   To: Victor Gastanaga <vgastan@almaak.usc.edu>
In-Reply-To:   <Pine.SV4.3.94.980402222221.29551A-100000@almaak.usc.edu>
Content-Type:   text/plain; charset="us-ascii"

It'll take two passes.

*BASICALLY* sort it like you've done (i.e., by ID FROM TO), retain a var called (e.g.) STAY, intitialize it to 1 on the (1st.ID) and then using LAG functions, check per each ID whether the 2nd record's FROM date is >= to the lagged FROM date from the 1st record *AND* <= the lagged TO date from the 1st record, if it is, then its in the same STAY, otherwise increment STAY by 1 and proceed, repeating for the 3rd - Nth obs for that ID.

NOTE: You'll actually need assign the LAG of the 1st records FROM date to a new var so that you're always able to refer to the 1st record's FROM date, and similarly a new var for the LAG 1st records' To date, *BUT*, this new *TO* date will be updated to the max of this new *TO* or the current records TO date to deal with situations where your 1st record doesn't define the entire Length of Saty (LOS) (e.g.,

ID PO FROM TO COST ROOM 1 i 01/01/95 01/03/95 9000 ICU 1 a 01/03/95 01/06/95 4500 M/S 1 a 03/05/95 03/05/95 3000 ???

i.e., you always want to make the comparison be one of the current record vs. the *earliest* FROM date (by definition the value of the 1st record's FROM date) and whatever the max TO date (up to that point).

After the first data step where you define STAY, then you can either run it through another data step, by ID STAY FROM TO, or do a Proc Summary with appropriate CLASS vars.

I personally like a 2nd data step to control the actual FROM and TO that are output (an ID in Proc Summary sometimes gets messy) as well as to set other flags (e.g., a ICU or CCU flag, a PSYCH flag, etc.)

Hope this helps

At 10:23 PM 4/2/98 -0800, Victor Gastanaga wrote: >Hello group, > >This is a nice (but hard!) problem I just came across. >This is my data set: > >ID PO FROM TO COST >1 i 01/01/95 01/05/95 90 >1 a 01/02/95 01/02/95 80 >1 a 03/05/95 03/05/95 30 >1 i 03/20/95 04/01/95 95 >2 a 01/15/95 01/15/95 44 >2 a 02/25/95 02/25/95 40 >etc. > >ID and PO are character, FROM and TO are dates, COST is numeric. Records >with PO="i" take several days (meaning TO > FROM), while records with >PO="a" usually take only one day (meaning FROM=TO). > >This what I want to do. If a PO="a" event takes place within the >FROM and TO dates of a PO="i" event, then I want to combine them into a >single "event", where the COST is added. In other words, I don't want to >have single records for PO="a" events that take place at the same time >as a long PO="i" event. However, PO="a" events that occur when no PO="i" >event takes place, are kept as spearate records. > >This is the outcome I want for the example above: > >ID EVENT FROM TO COST >1 i 01/01/95 01/05/95 170 >1 a 03/05/95 03/05/95 30 >1 a 03/20/95 04/01/95 95 >2 a 01/15/95 01/15/95 44 >2 a 02/25/95 02/25/95 40 > >Notice that all PO="i" and PO="a" events that take place between 01/01 and >01/05 (for ID=1) are combined into a single line with cost $170. > >Any suggestions? Thanks in advance. > >Victor > > /*=====================*/ W. W. Viergever Viergever & Associates Sacramento, CA (916) 923-2355 /*=====================*/


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