BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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
5 REPLIES 5
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
deleted_user
Not applicable
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
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
Andre
Obsidian | Level 7
Jeff

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

secondly : why hashing?

Andre
deleted_user
Not applicable
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;

Catch up on SAS Innovate 2026

Nearly 200 sessions are now available on demand with the SAS Innovate Digital Pass.

Explore Now →
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1853 views
  • 0 likes
  • 3 in conversation