Date: Fri, 11 Apr 2008 21:27:41 -0400 Muthia Kachirayan "SAS(r) Discussion" Muthia Kachirayan Re: how to deal with multiple ID <3a237784-cd86-45d6-a20f-491e08221473@t54g2000hsg.googlegroups.com> text/plain; charset=ISO-8859-1

On Fri, Apr 11, 2008 at 7:44 PM, AnoniDeer <irain168@gmail.com> wrote:

> I have a dataset like this.How could I get the difference between the > third and fourth observations for each of the group? for example for > the group 1 I need to get the difference for the observation of 45 and > 34. For the second group I need the difference of 76-34 and so on... > How to write a program to realize this? Thanks so much! > > group x > 1 23 > 1 . > 1 34 > 1 45 > 2 45 > 2 89 > 2 34 > 2 76 > 4 23 > 4 12 > 4 23 > 4 12 >

Here is a lengthy program. It does not assume that there should be 4 observations per group and that they need be together within group. Hash object is used to sense the group change. When the 3rd and 4th observations within a group are met they are used to find the difference and saved to output data set.

data have; input group x; cards; 1 23 1 . 1 34 1 45 2 45 2 89 2 34 2 76 4 23 4 12 4 23 4 12 ; run;

data need; if _n_ = 1 then do; if 0 then set have; declare hash h(); h.definekey('group'); h.definedone(); end; do until(eof); set have end = eof; if h.find() ne 0 then do; count = 1; x3 = 0; h.add(); end; else do; count ++ 1; if count = 3 then x3 = x; if count = 4 then do; x_34 = x - x3; output; end; end; end; stop; drop count x3; run; proc print data = need; run;

Muthia Kachirayan

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