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.