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 (October 2008, week 1)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Wed, 1 Oct 2008 15:51:48 -0500
Reply-To:     Joe Matise <snoopy369@GMAIL.COM>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         Joe Matise <snoopy369@GMAIL.COM>
Subject:      Re: Summation by ID and date
Comments: To: Lauren <maillauren@gmail.com>
In-Reply-To:  <4ad75669-b78e-4a1a-be25-94a6327fe7ac@75g2000hso.googlegroups.com>
Content-Type: text/plain; charset=ISO-8859-1

Sounds like you need to use first.visit and last.visit instead of first.id...

data new; set test; by id visit; if first.visit then do; totalcost=0; end; totalcost+cost; if last.visit then output; run;

That should do what you want (re-start it when a new date is reached).

Output (I changed one of the dates to make sure I wasn't misremembering how first/last works):

Obs id visit cost totalcost

1 1 04JUN2003 1 6 2 1 01AUG2003 2 52 3 2 01JAN2003 3 3 4 3 01JAN2003 8 15

-Joe

On Wed, Oct 1, 2008 at 3:25 PM, Lauren <maillauren@gmail.com> wrote:

> Hi All - > > This seems like it should be a simple task but I'm having problems > using the summation statement I would like to use (non-SQL). I want > the summary of cost each id, by visit. > > data test ; > input id visit :mmddyy8. cost ; > format visit date9. ; > cards ; > 1 6/4/03 2 > 1 6/4/03 3 > 1 6/4/03 1 > 1 8/1/03 50 > 1 8/1/03 2 > 2 1/1/03 3 > 3 2/3/03 7 > 3 2/3/03 8 > ; > > I hoped the code could be something along the lines of this but I need > the new visit date to start over with 0, sum those and then output the > last visit total for each id. > > data new; > set test; > by id visit; > if first.id then do; > totalcost=0; > end; > totalcost+cost; > if last.id then output; > run; > > Thanks! >


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