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.
Requirement The KEY:keyvalue arguments must be in the same order as they were defined in the hash object because the hash object variable names are not specified.

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.
Requirement The DATA:datavalue arguments must be in the same order as they were defined in the hash object because the hash object variable names are not specified.

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;
   length brd $10 plc $8;
   input brd plc;
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('brd', '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();
   /* Write the hash table to the data set. */
   rc = h.output(dataset: 'work.show');
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;
   length brd $10 plc $8;
   input brd plc;
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('brd', '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');
   /* Write the hash table to the data set. */
   rc = h.output(dataset: 'work.show');
run;
Note: The hash object's REPLACE method is intended for use with hash tables that have a single item for each key (MULTIDATA: 'NO'), whereas the REPLACEDUP method is intended for use with hash tables that have multiple data items for each key (MULTIDATA: 'YES'). In the SAS 9.4 release, if you call the REPLACE method and the hash object was declared using the multidata:'y' option, then all data items for the current key are replaced with the new data. In previous releases, no items are replaced and the new data is added to the current key.For more information about the MULTIDATA option, see DECLARE Statement, Hash and Hash Iterator Objects.
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 replaces only 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.