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
Quartz | Level 8

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

mohamed_zaki
Barite | Level 11

Then use the the option 

multidata="Y"

mariopellegrini
Quartz | Level 8

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
Quartz | Level 8

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 :).

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
  • 10 replies
  • 1267 views
  • 0 likes
  • 4 in conversation