Previous Page | Next Page

Hash and Hash Iterator Object Language Elements

REMOVE Method



Removes the data that is associated with the specified key from the hash object.
Applies to: Hash object

Syntax
Arguments
Details
Examples
See Also

Syntax

rc=object.REMOVE(<KEY: keyvalue-1,..., KEY: keyvalue-n>);


Arguments

rc

specifies whether the method succeeded or failed.

A return code of zero indicates success; a non-zero 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 only removes the value in the hash object.  [cautionend]

Note:   If you specify multidata:'y' in the hash object constructor, the REMOVE method will remove all data items for the specified key.  [cautionend]


Examples

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 table
   when an iterator is pointing to that key */
   do while (hi.next() = 0);
      if flag then rc=h.remove(key:key);
         if d = 'C' then do;
            key=k; 
            flag=1;
         end;
      end;

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

                                The SAS System                               1

                                Obs     k    d

                                 1     65    A
                                 2     66    B
                                 3     68    D
                                 4     69    E
                                 5     70    F

See Also

Methods:

ADD Method

DEFINEKEY Method

REMOVEDUP Method

Replacing and Removing Data in SAS Language Reference: Concepts

Previous Page | Next Page | Top of Page