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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register 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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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