REMOVE Method

Removes the data that is associated with the specified key from the hash object.

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.
Restriction If an associated hash iterator is pointing to the keyvalue, then the REMOVE method will not remove the key or data from the hash object. An error message is issued.

Details

The REMOVE method deletes both the key and the data from the hash object.
You can use the REMOVE method in one of two ways to remove the key and data in a hash object.
You can specify the key, and then use the REMOVE method as shown in the following code:
data _null_;
   length k $8;
   length d $12;
   if _N_ = 1 then do;
      declare hash h();
      rc = h.defineKey('k');
      rc = h.defineData('d');
      rc = h.defineDone();
      /* avoid uninitialized variable notes */
      call missing(k, d);
   end;
   rc = h.add(key: 'Joyce', data: 'Ulysses');
   /* Specify the key */
   k = 'Joyce';
   /* Use the REMOVE method to remove the key and data */
   rc = h.remove();
   if (rc = 0) then
      put 'Key and data removed from the hash object.';
run;
Alternatively, you can use a shortcut and specify the key directly in the REMOVE method call as shown in the following code:
data _null_;
   length k $8;
   length d $12;
   if _N_ = 1 then do;
      declare hash h();
      rc = h.defineKey('k');
      rc = h.defineData('d');
      rc = h.defineDone();
      /* avoid uninitialized variable notes */
      call missing(k, d);
   end;
   rc = h.add(key: 'Joyce', data: 'Ulysses');
   rc = h.add(key: 'Homer', data: 'Iliad');
   /* Specify the key in the REMOVE method parameter */
   rc = h.remove(key: 'Homer');
   if (rc  =0) then
      put 'Key and data removed from the hash object.';
run;
Note: The REMOVE method does not modify the value of data variables. It removes only the value in the hash object.
Note: If you specify multidata:'y' in the hash object constructor, the REMOVE method will remove all data items for the specified key.

Example: Removing a Key in the Hash Table

This example illustrates how to remove a key in the hash table.
/* Generate test data */
data x;
   do k = 65 to 70;
      d = byte (k);
      output;
   end;
run;
data _null_;
   length k 8 d $1;
   /* define the hash table and iterator */
   declare hash H (dataset:'x', ordered:'a');
   H.defineKey  ('k');
   H.defineData ('k', 'd');
   H.defineDone ();
   call missing (k,d);
   declare hiter HI ('H');
/*Use this logic to remove a key in the hash object when an*/
/*iterator is pointing to that key. The NEXT method will*/
/*start at the first item in the hash object if it is called*/
/*without calling the FIRST method. */
    do while (hi.next() = 0);
      if flag then rc=h.remove(key:key);
         if d = 'C' then do;
            key=k;
            flag=1;
         end;
         else flag=0;
      end;
  if flag then rc=h.remove(key:key);
  rc = h.output(dataset: 'work.out');
  stop;
run;
proc print;
run;
The following output shows that the key and data for the third object (key=67, data=C) is deleted.
Key and Data Removed from Output
Key and Data Removed from Output

See Also

Replacing and Removing Data in the Hash Object in SAS Language Reference: Concepts