BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

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?

1 ACCEPTED SOLUTION

Accepted Solutions
mohamed_zaki
Barite | Level 11

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

10 REPLIES 10
mohamed_zaki
Barite | Level 11

Give example of your desired output....

mariopellegrini
Pyrite | Level 9

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

mohamed_zaki
Barite | Level 11

Then use the the option 

multidata="Y"

mariopellegrini
Pyrite | Level 9

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;
mohamed_zaki
Barite | Level 11

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;

mariopellegrini
Pyrite | Level 9

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

data_null__
Jade | Level 19
multidata:'Y'



RTM

Haikuo
Onyx | Level 15

@data_null__ wrote:
multidata:'Y'

RTM

Wondering when SAS forum gonna fix this.

data_null__
Jade | Level 19
I modified to make it CODE but that didn't fix it. 😞
Haikuo
Onyx | Level 15

It has been consistent :).

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

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