```Date: Wed, 8 Apr 2009 13:21:28 -0400 Reply-To: oloolo Sender: "SAS(r) Discussion" From: oloolo Subject: Re: Means On Wed, 8 Apr 2009 12:04:09 -0400, Randy wrote: >My data is as follows: > >date time ID VarA >Mar1 9:00 XA 45 >Mar1 9:00 XA 16 >Mar1 10:00 XA 34 >Mar1 10:01 XB 16 >Mar1 10:02 XD 17 >Mar1 10:05 XM 18 >Mar1 10:05 XM 19 >Mar1 10:34 XM 20 > >I want to sum and get the average of these variables. But every ID at the >same time has to be grouped as one, before I take the average of these >variables. So for example, on Mar1 XA at 9:00 has to be grouped as one, and >so should XM at 10:05. So the data set should first look like before I take >the averages. > date time ID VarA >Mar1 9:00 XA 61 >Mar1 10:00 XA 34 >Mar1 10:01 XB 16 >Mar1 10:02 XD 17 >Mar1 10:05 XM 37 >Mar1 10:34 XM 20 > >This operation can be performed in two data steps. Is there a way to do it >in one data step? > Randy Just realized that OP might want avg per Time+ID group, then you can go like this: ****************; data _null_; length ID \$ 2; if _n_=1 then do; declare hash h(); h.defineKey('time', 'ID'); h.defineData('time', 'ID', 'count', 'mean'); h.defineDone(); sum=0; do until (eof); set have end=eof; rc=h.find(); if rc=0 then do; count+1; mean=(count-1)*mean/count + VarA/count; rc=h.replace(); end; else do; sum=VarA; count=1; mean=VarA; rc=h.add(); end; end; end; ro=h.output(dataset: 'mean2'); stop; run; ```

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