You can use the REPLACE
method in one of two ways to replace data in a hash object.
You can define the key
and data item, and then use the REPLACE method as shown in the following
code. In this example the data for the key 'Rottwlr' is changed from
'1st' to '2nd'.
data work.show;
input brd $1-10 plc $12-14;
datalines;
Terrier 2nd
LabRetr 3rd
Rottwlr 1st
Collie bis
ChinsCrstd 2nd
Newfnlnd 3rd
;
proc print data=work.show;
title 'SHOW Data Set Before Replace';
run;
data _null_;
length brd $12;
length plc $8;
if _N_ = 1 then do;
declare hash h(dataset: 'work.show');
rc = h.defineKey('brd');
rc = h.defineData('plc');
rc = h.defineDone();
end;
/* Specify the key and new data value */
brd = 'Rottwlr';
plc = '2nd';
/* Call the REPLACE method to replace the data value */
rc = h.replace();
run;
proc print data=work.show;
title 'SHOW Data Set After Replace';
run;
Alternatively, you can
use a shortcut and specify the key and data directly in the REPLACE
method call as shown in the following code:
data work.show;
input brd $1-10 plc $12-14;
datalines;
Terrier 2nd
LabRetr 3rd
Rottwlr 1st
Collie bis
ChinsCrstd 2nd
Newfnlnd 3rd
;
data _null_;
length brd $12;
length plc $8;
if _N_ = 1 then do;
declare hash h(dataset: 'work.show');
rc = h.defineKey('brd');
rc = h.defineData('plc');
rc = h.defineDone();
/* avoid uninitialized variable notes */
call missing(brd, plc);
end;
/* Specify the key and new data value in the REPLACE method */
rc = h.replace(key: 'Rottwlr', data: '2nd');
run;
Note: If you call the REPLACE method
and the key is not found, then the key and data are added to the hash
object.
Note: The REPLACE method does not
replace the value of the data variable with the value of the data
item. It only replaces the value in the hash object.