DATA Step, Macro, Functions and more

Hash Object Use Selected Key Only

Accepted Solution Solved
Reply
Contributor
Posts: 33
Accepted Solution

Hash Object Use Selected Key Only

Good morning.

In the guide sas (https://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002576871.htm) is an example of how the option 'replace' stores the last duplicate key record, but it does not show how to record in the object hash values of duplicate key:

 

data table;
  input key data $;
  datalines;
  531 yellow
  620 green
  531 blue
  908 orange
  620 brown
  143 purple
 run;

data _null_;
length key 8 data $ 8;
if (_n_ = 1) then do;
    declare hash myhash(dataset: "table", duplicate: "r");
    rc = myhash.definekey('key');
    rc = myhash.definedata('data');
    myhash.definedone();
 end;

rc = myhash.output(dataset:"otable");
run;

 

I would get in the dataset 'table', the values of duplicate key. It's possible?


Accepted Solutions
Solution
‎12-17-2015 08:55 AM
Super Contributor
Posts: 490

Re: Hash Object Use Selected Key Only

[ Edited ]

The hash object keeps the multiple values in a list that is associated with the key. This list can be traversed and manipulated by using several methods such as HAS_NEXT or FIND_NEXT.

 

See the first example in Better Hashing in SAS® 9.2

 

 

 

data _null_;
length key 8 data $ 8;
if (_n_ = 1) then do;
declare hash myhash(dataset: "table",multidata: "y");

rc = myhash.definekey('key');
rc = myhash.definedata('data');
myhash.definedone();
end;

do key = '531', '620', '908', '143';
rc = myhash.find();
if (rc = 0) then do;
put key= @15 data=;
myhash.has_next(result: r);
do while(r ne 0);
myhash.find_next();
put key= @15 data=  @25 '(dup)';
myhash.has_next(result: r);
end;
end;
end;
run;

View solution in original post


All Replies
Super Contributor
Posts: 490

Re: Hash Object Use Selected Key Only

Give example of your desired output....

Contributor
Posts: 33

Re: Hash Object Use Selected Key Only

a dataset identical to the original one (table), including duplicate data

Super Contributor
Posts: 490

Re: Hash Object Use Selected Key Only

Then use the the option 

multidata="Y"

Contributor
Posts: 33

Re: Hash Object Use Selected Key Only

I've already tried, but I do not get duplicates:

 

data table;
  input key data $;
  datalines;
  531 yellow
  620 green
  531 blue
  908 orange
  620 brown
  143 purple
 run;

data _null_;
length key 8 data $ 8;
if (_n_ = 1) then do;
    declare hash myhash(dataset: "table",   multidata = 'Y');
    rc = myhash.definekey('key');
    rc = myhash.definedata('data');
    myhash.definedone();
 end;

rc = myhash.output(dataset:"otable");
run;
Solution
‎12-17-2015 08:55 AM
Super Contributor
Posts: 490

Re: Hash Object Use Selected Key Only

[ Edited ]

The hash object keeps the multiple values in a list that is associated with the key. This list can be traversed and manipulated by using several methods such as HAS_NEXT or FIND_NEXT.

 

See the first example in Better Hashing in SAS® 9.2

 

 

 

data _null_;
length key 8 data $ 8;
if (_n_ = 1) then do;
declare hash myhash(dataset: "table",multidata: "y");

rc = myhash.definekey('key');
rc = myhash.definedata('data');
myhash.definedone();
end;

do key = '531', '620', '908', '143';
rc = myhash.find();
if (rc = 0) then do;
put key= @15 data=;
myhash.has_next(result: r);
do while(r ne 0);
myhash.find_next();
put key= @15 data=  @25 '(dup)';
myhash.has_next(result: r);
end;
end;
end;
run;

Contributor
Posts: 33

Re: Hash Object Use Selected Key Only

This method is intricate, it is perhaps best to add the variable _N_ the key object to not have duplicates?

Respected Advisor
Posts: 3,777

Re: Hash Object Use Selected Key Only

[ Edited ]
multidata:'Y'



RTM

Respected Advisor
Posts: 3,124

Re: Hash Object Use Selected Key Only

[ Edited ]

data_null__ wrote:
multidata:'Y'

RTM

Wondering when SAS forum gonna fix this.

Respected Advisor
Posts: 3,777

Re: Hash Object Use Selected Key Only

I modified to make it CODE but that didn't fix it. :-(
Respected Advisor
Posts: 3,124

Re: Hash Object Use Selected Key Only

It has been consistent Smiley Happy.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 311 views
  • 0 likes
  • 4 in conversation