Date: Thu, 29 Oct 2009 10:43:18 -0400
Reply-To: Jeff <zhujp98@GMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Jeff <zhujp98@GMAIL.COM>
Subject: Re: dynamic data set?
In-Reply-To: <6716d5d0910290654i235747f4oba6108225fb2e859@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
I tried following but did not get the expected result.
Thanks in advance for any suggestions on how to make this to work,
Jeff
data d2;
retain id;
declare hash h();
rc=h.definekey('id2');
rc=h.definedata('x');
rc=h.definedone();
set d1;
if (id1 ne id and h.find() ne 0) then do;
id=id1;
rc=h.add();
output;
end;
run;
On Thu, Oct 29, 2009 at 9:54 AM, Jeff <zhujp98@gmail.com> wrote:
> Thank you for you response. Sorry I did not stat the problem clearly
> Basically.
> I have dataset d1 and I want to get d2
> *
>
> data
> * d1;
>
> input
> id1 id2 x;
>
> cards
> ;
>
> 1 2 10
>
> 1 3 8
>
> 1 5 7
>
> 1 1 6
>
> 2 2 10
>
> 2 8 5
>
> 2 20 4
>
> 3 8976 50
>
> 3 4 48
>
> 4 4 48
>
> 4 982 48
>
> 5 4 48
>
> 5 982 48
>
> 6 4 48
>
> 6 2 10
>
> 7 4 48
>
> 7 2 10
>
> 7 8 5
>
> 7 47 4
>
> ;
> *
>
> run
> *;
> *
>
> expected d2:
>
> 1
> * *2* *10 * 1st obs in d2;
>
> 2
> * *8* *5 * i do not want 2 2 10, because id2=2
> already in d2.
>
> 3
> *
> *8976* *50
>
> 4
> * *4* *48
>
> 5
> * *982* *48
>
> 7
> *
> *47* *4 *id2= 4 2 8 already in d2.*
>
>
>
> The code tried
>
> data d2;
>
> set
> d1;
>
> retain
> id;
>
> *declare hash h(); -- i want o declear a hash table or array only contain
> d2
>
> if
> (id1 ne id /*and id2 not in h*/) then do;
>
> id=id1;
>
> /*push id 2 into h like h.add(id2) */
>
> output
> ;
>
> end
> ;*
>
> run
> *;
> I do not know how can a hash table works here.
> Any more suggestion will be greatly appreciated,
> Thanks
> Jeff
>
>
>
> > data d1;
> > input id1 id2 x;
> > cards;
> > 1 2 10
> > 1 3 8
> > 1 5 7
> > 1 1 6
> > 2 2 10
> > 2 8 5
> > 2 20 4
> > 3 8976 50
> > 3 4 48
> > 4 4 48
> > 4 982 48
> > 5 4 48
> > 5 982 48
> > 6 4 48
> > 6 2 10
> > 7 4 48
> > 7 2 10
> > 7 8 5
> > 7 47 4
> > ;
> > run;
> >
> > the result data d2 will be like
> > id1 id2 x
> > 1 2 10
> > 2 8 5
> > 3 8976 50
> > 4 4 48
> > 5 982 48
> > 7 47 4
>
>
> On Thu, Oct 29, 2009 at 9:20 AM, Jack Hamilton <jfh@stanfordalumni.org>wrote:
>
>> It's not obvious (to me, anyway) what the rules are.
>>
>> If you're saying each value me id2 can appear in the output only once,
>> you'll need to keep track of what has already appeared; an array or hash
>> object could be used for that. You probably need a double DOW loop as well.
>>
>>
>>
>>
>> --
>> Jack Hamilton
>> jfh@alumni.stanford.org
>>
>> -----Original Message-----
>> From: Jeff <zhujp98@GMAIL.COM>
>> Sent: Thursday, October 29, 2009 5:55 AM
>> To: SAS-L@LISTSERV.UGA.EDU
>> Subject: Re: [SAS-L] dynamic data set?
>>
>> Any one can give me some hints on how to solve this problem.
>> Any suggestion will be greatly appreciated.
>>
>>
>> On Wed, Oct 28, 2009 at 11:38 PM, Jeff <zhujp98@gmail.com> wrote:
>>
>> > I have a dataset d1sorted by id an x with several thousand record.
>> > here is a small part of it.
>> > data d1;
>> > input id1 id2 x;
>> > cards;
>> > 1 2 10
>> > 1 3 8
>> > 1 5 7
>> > 1 1 6
>> > 2 2 10
>> > 2 8 5
>> > 2 20 4
>> > 3 8976 50
>> > 3 4 48
>> > 4 4 48
>> > 4 982 48
>> > 5 4 48
>> > 5 982 48
>> > 6 4 48
>> > 6 2 10
>> > 7 4 48
>> > 7 2 10
>> > 7 8 5
>> > 7 47 4
>> > ;
>> > run;
>> >
>> > the result data d2 will be like
>> > id1 id2 x
>> > 1 2 10
>> > 2 8 5
>> > 3 8976 50
>> > 4 4 48
>> > 5 982 48
>> > 7 47 4
>> > .........
>> >
>> > The rules is:
>> > x1 contains the largest possible value of each id1 while satifies this
>> > condition: in d2, id2 has no duplicates.
>> > which means if id2 is a obs is already in d2, we move to the next obs
>> with
>> > the next largest x.
>> >
>> > How can I get the the resulting dataset?
>> > Thanks.
>> > Jeff
>> >
>> >
>> >
>> >
>> >
>> >
>>
>
>
|