REPLACE Method

Replaces the data that is associated with the specified key with new data.
Applies to: Hash object

Syntax

Arguments

rc
specifies whether the method succeeded or failed.
A return code of zero indicates success; a nonzero value indicates failure. If you do not supply a return code variable for the method call and the method fails, then an appropriate error message is written to the log.
object
specifies the name of the hash object.
KEY: keyvalue
specifies the key value whose type must match the corresponding key variable that is specified in a DEFINEKEY method call.
The number of “KEY: keyvalue” pairs depends on the number of key variables that you define by using the DEFINEKEY method.
DATA: datavalue
specifies the data value whose type must match the corresponding data variable that is specified in a DEFINEDATA method call.
The number of “DATA: datavalue” pairs depends on the number of data variables that you define by using the DEFINEDATA method.

Details

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.

Comparisons

The REPLACE method replaces the data that is associated with the specified key with new data. The REPLACEDUP method replaces the data that is associated with the current key's current data item with new data.