The Replacedup Method lets us replace and update single items in a SAS hash object. From SAS 9.4 and forward, the Replace Method replaces entire key groups. The Replacedup Method is not frequently used. Therefore, there does not exist much information about the method. In this post, I will demonstrate how do use it with a few examples.

In the examples to come, I will use the SAS data set below.

data have;
input k d;
datalines;
1 10
1 20
1 30
2 5 
2 10
2 15
3 20
3 30
3 40
;

A Simple Example

First, let us take a look at a simple Replacedup Method example. In the snippet below, I create a simple hash object with the keys k and the data variables k and d. I use the Find() Method to copy the data values in the first item in the same-key group for k=2. Next, the Find_Next() call points to the second item in the k=2 group. Finally, I call the Replacedup Method. I specify the data values k and 100 for the update values. Notice that this updates the item with the current PDV values of k and the plain value 100 for k and d respectively. This updates the item that we currently point to. And only that item. At the bottom, I print the content of the hash object. The Replacedup Method succesfully updates the second item in the k=2 group.

As a side not, notice that I use uassigned method calls below. I do so only because I know for a fact that the calls are succesful for the

data _null_;
   declare hash h(dataset : "have", multidata : "Y", ordered : "A");
   h.definekey("k");
   h.definedata("k", "d");
   h.definedone();
   declare hiter hi("h");
 
   k = .; d = .;
 
   h.find(key : 2);
   h.find_next();
   h.replacedup(data : k, data : 100);
 
   do while (hi.next()=0);
      put (k d)(=);
   end;
run;

Selectively Updating Multiple Items

Next, let us see how to update multiple items within item groups. Suppose we want to update items if the value of the data variable d is 20 or 30. We can achieve this with a simple do loop and Find()/Find_Next() logic. Run the code below and verify that we replace and update the desired items.

If you read the related post Explore the SAS Hash Object Removedup Method, you will learn that a successful Removedup call unsets the item-list that it currently dwells on. This is not the case with the Replacedup Method. Consequently, we do not need to jump the same hoops to get around this issue. The logic below works. Regardless of whether a single or multiple items meet the criteria.

data _null_;
   declare hash h(dataset : "have", multidata : "Y", ordered : "A");
   h.definekey("k");
   h.definedata("k", "d");
   h.definedone();
   declare hiter hi("h");
 
   k = .; d = .;
 
   do k = 1, 3;
      do rc = h.find() by 0 while (rc = 0);
         if d in (20, 30) then rc2 = h.replacedup(data : k, data : 100);
         rc = h.find_next();
      end;
   end;
 
   do while (hi.next()=0);
      put (k d)(=);
   end; 
run;

The SAS Do_Over Method

In the post about the Removedup Method, we learn that the Do_Over Method prevents the unsetting of the item-list for a succesful method call. Since this is not the case for the Replacedup Method, we do not have the same upside with Do_Over. However, the Do_Over Method call still simplifies the code a bit. Run the code below and verify that the result is the same.

data _null_;
   declare hash h(dataset : "have", multidata : "Y", ordered : "A");
   h.definekey("k");
   h.definedata("k", "d");
   h.definedone();
   declare hiter hi("h");
 
   k = .; d = .;
 
   do k = 1, 3;
      do while (h.do_over() = 0);
         if d in (20, 30) then rc2 = h.replacedup(data : k, data : 100);
      end;
   end;
 
   do while (hi.next()=0);
      put (k d)(=);
   end; 
run;

Summary

In this post, I demonstrate how to use the Replacedup Method in the SAS Hash Object. I demonstrate a few examples and demonstrate that we can update single items with the method. In contrast to the regular Replace Method, which replaces entire same-key groups.

For further readinf, I encourage you to browse the Replacedup Documentation and read the proper literature such as Data Management Solutions Using SAS Hash Table Operations.

You can download the entire code from this post here.