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?
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;
Give example of your desired output....
a dataset identical to the original one (table), including duplicate data
Then use the the option
multidata="Y"
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;
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;
This method is intricate, it is perhaps best to add the variable _N_ the key object to not have duplicates?
multidata:'Y'
RTM
@data_null__ wrote:
multidata:'Y'
RTM
Wondering when SAS forum gonna fix this.
It has been consistent :).
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.