| Date: | Sat, 12 Apr 2008 12:34:37 -0700 |
| Reply-To: | Daniel Nordlund <djnordlund@VERIZON.NET> |
| Sender: | "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU> |
| From: | Daniel Nordlund <djnordlund@VERIZON.NET> |
| Subject: | Re: how to deal with multiple ID |
|
| In-Reply-To: | <7dd5cc870804121027j37c4210age2192e932576781c@mail.gmail.com> |
| Content-Type: | text/plain; charset="utf-8" |
It is best to reply to the list, rather than to me directly as your email may get lost in my spam filters, and you lose the benefit of all the sharp eyes on SAS-L who can correct any errors or inefficiencies I might try to pass off on you. :-) You should read up on “BY-variable processing”. To get the difference between the first and last record by group can be done by capturing the first value and “retaining” it till you have the last record per group.
data want ;
set have;
by group;
if first.group then first_value = x;
retain first_value;
if last.group then do;
diff_last_first = x – first_value;
output;
end;
run;
Hope this is helpful,
Dan
Daniel Nordlund
Bothell, WA USA
_____
From: Properly [mailto:irain168@gmail.com]
Sent: Saturday, April 12, 2008 10:27 AM
To: Daniel Nordlund
Subject: Re: how to deal with multiple ID
Hi, Daniel!
Thanks so much for your reply. This is just what I need.
By the way, do you have any ideal how to do with the difference between first obs and last obs of the same group?
Thanks very much!
Regards,
On Fri, Apr 11, 2008 at 8:04 PM, Daniel Nordlund <djnordlund@verizon.net> wrote:
> -----Original Message-----
> From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of
> AnoniDeer
> Sent: Friday, April 11, 2008 4:44 PM
> To: SAS-L@LISTSERV.UGA.EDU
> Subject: how to deal with multiple ID
>
> 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!
>
I am assuming that your sample data faithfully represents what your data actually looks like: in sorted order, each group having exactly 4 observations. Something like this will work
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 want ;
set have;
by group;
diff_34 = dif(x);
if last.group then output;
run;
If this doesn't meet your needs, write back to the list with more detail.
Hope this is helpful,
Dan
Daniel Nordlund
Bothell, WA USA
--
Regards,
Properly
|