The SAS Output Delivery System and reporting techniques

how to get this hash table work?

Reply
N/A
Posts: 0

how to get this hash table work?

I have dataset d1 sorted by id1 and x.

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;

I want to get a new data d2 which is a sunset of d1
id1 id2 x
1 2 10
2 8 5
3 8976 50
4 4 48
5 982 48
7 47 4

I tried this code with hash table. but it does not work.

data d2;
retain id;
declare hash h();
rc=h.definekey('id2');
rc=h.definedata('x');
rc=h.definedone();
set d1;
rr=h.find(key:id2);
if (id1 ne id and rr ne 0) then do;
id=id1;
h.add(key:id2, data:x);

output;
end;
run;


How can I fix this problem?
Thanks.
Jeff
Super Contributor
Super Contributor
Posts: 3,174

Re: how to get this hash table work?

Posted in reply to deleted_user
It's difficult to fully understand what you want to accomplish "in words" rather than just showing OUTPUT data - for example, what happened to id1 value 6?

Also, paste your SAS-generated logs and suggest you use some diagnostic SAS commands, like:

PUTLOG _ALL_;

in your program for self-diagnosis and desk-checking.

Scott Barry
SBBWorks, Inc.
N/A
Posts: 0

Re: how to get this hash table work?

here is the rule:
i want to get the first largest possible x for each id1 while id2 in d2 can not have duplicates.

I want to get a new data d2 which is a sunset of d1
id1 id2 x
1 2 10 * first record*;
2 8 5 * 2 2 10 can not be in d2 because id2=2 already in d2*
3 8976 50
4 4 48
5 982 48 *5 4 48 is not in d2 becuase id=t already in d2. and id1=6 has no obs in d2 because id2=4, 2 already in d2.
7 47 4

Thanks.
Jeff
Super Contributor
Super Contributor
Posts: 3,174

Re: how to get this hash table work?

Posted in reply to deleted_user
I don't see any up-front SAS code that loads your hash table.

Suggest you back away from using hash tables and get your logic working using either a _temporary_ array or a SAS PROC FORMAT approach.

Scott Barry
SBBWorks, Inc.
Super Contributor
Posts: 273

Re: how to get this hash table work?

Posted in reply to deleted_user
Jeff

It seems for me that you are asking something without explaning any rules
http://en.wikipedia.org/wiki/Sunset

secondly : why hashing?

Andre
N/A
Posts: 0

Re: how to get this hash table work?

Posted in reply to deleted_user
I found the solution which generated the result i want
Thanks.
Jeff

data d2;
retain id;
id=0;
declare hash h();
rc=h.definekey('id2');
rc=h.definedata('x');
rc=h.definedone();
do until (eof);
set d1 end=eof;
if (id ne id1) then do;
rc1=h.add();
if rc1=0 then do;
id=id1;
output;
end;
end;
end;
run;
Ask a Question
Discussion stats
  • 5 replies
  • 188 views
  • 0 likes
  • 3 in conversation