LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (September 2008, week 4)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:   Fri, 26 Sep 2008 02:21:16 -0700
Reply-To:   phrising@YAHOO.DK
Sender:   "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:   phrising@YAHOO.DK
Organization:   http://groups.google.com
Subject:   Having sql min(date)
Comments:   To: sas-l@uga.edu
Content-Type:   text/plain; charset=ISO-8859-1

Dear group I got this dataset

Data sample; Input id id2 date1 date2; Cards; 1 2 11jan93 28jan93 1 4 14jan93 24jan93 1 6 16jan93 22jan93 2 8 02feb93 26feb93 2 10 12feb93 19feb93 2 12 15feb93 18feb93 ; Run;

I want to make a new dataset with the minimum date within each by group for date1 and date2. None of the dates are missing but the minimum date can occur more than once within each group. I want to do this using sql hoping to do it all in one step, I know it can be achieved using a datastep solution.

I want the final dataset to look like this

Id id2 date1 date2 date1_x date2_y 1 2 11jan93 28jan93 14jan93 22jan93 1 4 14jan93 24jan93 11jan93 22jan93 1 6 16jan93 22jan93 11jan93 24jan93 2 8 02feb93 26feb93 12feb93 18feb93 2 10 12feb93 19feb93 02feb93 18feb93 2 12 15feb93 18feb93 02feb93 19feb93

I got this code: Proc sql; Create table test (Select a.id, a.id2, a.date1, a.date2, b.date1 as date1_x where a.id=b.id and a.id2 ^= b.id2 from sample a, sample b group by a.id, a.id2 having b.date=min(b.date)) outer union (select b.date2 as date2_y Where a.id=b.id and a.id2 ^= b.id2 from sample a, sampe b group by a.id, a.id2 having b.date2=min(b.date2)) ; quit;

but this code results in this, which is quite close to what I want to achieve, except that I want to join and not append Id id2 date1 date2 date1_x date2_y 1 2 11jan93 28jan93 14jan93 . 1 4 14jan93 24jan93 11jan93 . 1 6 16jan93 22jan93 11jan93 . . . . . . 22jan93 . . . . . 22jan93 . . . . . 24jan93


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