REPLACEDUP Method

Replaces the data that is associated with the current key's current data item 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.
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 for the current key.

Details

You can use the REPLACEDUP method in one of two ways to replace data in a hash object.
You can define the data item, and then use the REPLACEDUP method. Alternatively, you can use a shortcut and specify the data directly in the REPLACEDUP method call.
Note: If you call the REPLACEDUP method and the key is not found, then the key and data are added to the hash object.
Note: The REPLACEDUP 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 REPLACEDUP method replaces the data that is associated with the current key's current data item with new data. The REPLACE method replaces the data that is associated with the specified key with new data.

Example: Replacing Data in the Current Key

This example creates a hash object where several keys have multiple data items. When a duplicate data item is found, 300 is added to the value of the data item.
data testdup;
   length key data 8;
   input key data;
   datalines;
   1 10
   2 11
   1 15
   3 20
   2 16
   2 9
   3 100
   5 5
   1 5
   4 6
   5 99
;
data _null_;
   length r 8;
   dcl hash h(dataset:'testdup', multidata: 'y', ordered: 'y');
   h.definekey('key');
   h.definedata('key', 'data');
   h.definedone();
   call missing (key, data);
   do key = 1 to 5; 
      rc = h.find();
      if (rc = 0) then do;
         put key= data=;
         h.has_next(result: r);
         do while(r ne 0);
            rc = h.find_next();
            put 'dup ' key= data;
            data = data + 300;
            rc = h.replacedup();
            h.has_next(result: r);
         end;
      end;
   end;
   put 'iterating...';
   dcl hiter i('h');
   rc = i.first();
   do while (rc = 0);
      put key= data=;
      rc = i.next();
    end;
run;
The following lines are written to the SAS log.
key=1 data=10
dup key=1 15
dup key=1 5
key=2 data=11
dup key=2 16
dup key=2 9
key=3 data=20
dup key=3 100
key=4 data=6
key=5 data=5
dup key=5 99
iterating...
key=1 data=10
key=1 data=315
key=1 data=305
key=2 data=11
key=2 data=316
key=2 data=309
key=3 data=20
key=3 data=400
key=4 data=6
key=5 data=5
key=5 data=399

See Also

Non-Unique Key and Data Pairs in SAS Language Reference: Concepts
Methods: