FIND Method

Determines whether the specified key is stored in 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.

Details

You can use the FIND method in one of two ways to find data in a hash object.
You can specify the key, and then use the FIND method as shown in the following code:
data _null_;
   length k $8;
   length d $12;
   /* Declare hash object and key and data variables */
   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;
   /* Define constant key and data values */
   rc = h.add(key: 'Joyce', data: 'Ulysses');
   /* Find the key JOYCE */
   k = 'Joyce';
   rc = h.find();
   if (rc = 0) then
      put 'Key is in the hash object.';
run;
Alternatively, you can use a shortcut and specify the key directly in the FIND method call as shown in the following code:
data _null_;
   length k $8;
   length d $12;
   /* Declare hash object and key and data variables */
   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;
    /* Define constant key and data values */
   rc = h.add(key: 'Joyce', data: 'Ulysses');
  /* Find the key JOYCE */
   rc = h.find(key: 'Joyce');
   if (rc = 0) then
      put 'Key is in the hash object.';
run;
If the hash object has multiple data items for each key, use the FIND_NEXT Method and the FIND_PREV Method in conjunction with the FIND method to traverse a multiple data item list.

Comparisons

The FIND method returns a value that indicates whether the key is in the hash object. If the key is in the hash object, then the FIND method also sets the data variable to the value of the data item so that it is available for use after the method call. The CHECK method only returns a value that indicates whether the key is in the hash object. The data variable is not updated.

Example: Using the FIND Method to Find the Key in a Hash Object

The following example creates a hash object. Two data values are added. The FIND method is used to find a key in the hash object. The data value is returned to the data set variable that is associated with the key.
data _null_;
   length k $8;
   length d $12;
   /* Declare hash object and key and data variable names */
   if _N_ = 1 then do;
      declare hash h();
      rc = h.defineKey('k');
      rc = h.defineData('d');
      /* avoid uninitialized variable notes */
      call missing(k, d);
      rc = h.defineDone();
   end;
   /* Define constant key and data values and add to hash object */
   rc = h.add(key: 'Joyce', data: 'Ulysses');
   rc = h.add(key: 'Homer', data: 'Odyssey');
   /* Verify that key JOYCE is in hash object and */
   /* return its data value to the data set variable D */
   rc = h.find(key: 'Joyce');
   put d=;
run;
d=Ulysses is written to the SAS log.