You can use the ADD
method in one of two ways to store data in a hash object.
You can define the key
and data item, and then use the ADD method as shown in the following
code:
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');
rc = h.defineDone();
end;
/* Define constant key and data values */
k = 'Joyce';
d = 'Ulysses';
/* Add key and data values to hash object */
rc = h.add();
run;
Alternatively, you can
use a shortcut and specify the key and data directly in the ADD method
call as shown in the following code:
data _null_;
length k $8;
length d $12;
/* Define hash object and key and data variable names */
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 and add to hash object */
rc = h.add(key: 'Joyce', data: 'Ulysses');
run;
If you add a key that
is already in the hash object, then the ADD method will return a nonzero
value to indicate that the key is already in the hash object. Use
the REPLACE method to replace the data that is associated with the
specified key with new data.
If you do not specify
the data variables with the DEFINEDATA method, the data variables
are automatically assumed to be same as the keys.
If you use the KEY:
and DATA: argument tags to specify the key and data directly, you
must use both argument tags.
The ADD method does
not set the value of the data variable to the value of the data item.
It only sets the value in the hash object.