put 'Add failed.';
/* Define constant value for key and data */
k = 'Homer';
d = 'Odyssey';
/* Use the ADD method to add the key and data to the hash object */
rc = h.add();
if (rc ne 0) then
put 'Add failed.';
/* Use the REPLACE method to replace 'Odyssey' with 'Iliad' */
k = 'Homer';
d = 'Iliad';
rc = h.replace();
if (rc = 0) then
put d=;
else
put 'Replace not successful.';
/* Use the REMOVE method to remove the 'Joyce' key and data */
k = 'Joyce';
rc = h.remove();
if (rc = 0) then
put k 'removed from hash object';
else
put 'Deletion not successful.';
run;
The following lines are written to the SAS log.
d=Iliad
Joyce removed from hash object
Note: If an associated hash iterator is pointing to the key, the REMOVE method does
not remove the key or data from the hash object. An error message is issued to the
log.
For more information, see the “REMOVE Method” in SAS Component Objects:
Reference, “REMOVEDUP Method” in SAS Component Objects: Reference,
“REPLACE Method” in SAS Component Objects: Reference, and the “REPLACEDUP
Method” in SAS Component Objects: Reference.
Saving Hash Object Data in a Data Set
You can create a data set that contains the data in a specified hash object by using the
OUTPUT method. In the following example, two keys and data are added to the hash
object and then output to the Work.Out data set.
options pageno=1 nodate;
data test;
length d1 8;
length d2 $20;
length k1 $20;
length k2 8;
/* Declare the hash object and two key and data variables */
528 Chapter 22 Using DATA Step Component Objects
if _N_ = 1 then do;
declare hash h();
rc = h.defineKey('k1', 'k2');
rc = h.defineData('d1', 'd2');
rc = h.defineDone();
end;
/* Define constant value for key and data */
k1 = 'Joyce';
k2 = 1001;
d1 = 3;
d2 = 'Ulysses';
rc = h.add();
/* Define constant value for key and data */
k1 = 'Homer';
k2 = 1002;
d1 = 5;
d2 = 'Odyssey';
rc = h.add();
/* Use the OUTPUT method to save the hash object data to the OUT data set */
rc = h.output(dataset: "work.out");
run;
proc print data=work.out;
run;
The following output shows the report that PROC PRINT generates.
Output 22.2 Data Set Created from the Hash Object
Note that the hash object keys are not automatically stored as part of the output data set.
The keys can be defined as data items by using the DEFINEDATA method to be
included in the output data set. In addition, if no data items are defined by using the
DEFINEDATA method, the keys are written to the data set specified in the OUTPUT
method. In the previous example, the DEFINEDATA method would be written this way:
rc = h.defineData('k1', 'k2', 'd1', 'd2');
For more information, see the “OUTPUT Method” in SAS Component Objects:
Reference.
Using the Hash Object 529
Comparing Hash Objects
You can compare one hash object to another by using the EQUALS method. In the
following example, two hash objects are being compared. Note that the EQUALS
method has two argument tags. The HASH argument tag is the name of the second hash
object. The RESULTS argument tag is a numeric variable name that holds the result of
the comparison (1 if equal and zero if not equal).
length eq k 8;
declare hash myhash1();
myhash1.defineKey('k');
myhash1.defineDone();
declare hash myhash2();
myhash2.defineKey('k');
myhash2.defineDone();
rc = myhash1.equals(hash: 'myhash2', result: eq);
For more information, see the “EQUALS Method” in SAS Component Objects:
Reference.
Using Hash Object Attributes
You can use the DATA Step Component Interface to retrieve information from a hash
object using an attribute. Use the following syntax for an attribute:
attribute_value=obj.attribute_name;
There are two attributes available to use with hash objects. NUM_ITEMS returns the
number of items in a hash object and ITEM_SIZE returns the size (in bytes) of an item.
The following example retrieves the number of items in a hash object:
n = myhash.num_items;
The following example retrieves the size of an item in a hash object:
s = myhash.item_size;
You can obtain an idea of how much memory the hash object is using with the
ITEM_SIZE and NUM_ITEMS attributes. The ITEM_SIZE attribute does not reflect the
initial overhead that the hash object requires, nor does it take into account any necessary
internal alignments. Therefore, the use of ITEM_SIZE does not provide exact memory
usage, but it gives a good approximation.
For more information, see the “NUM_ITEMS Attribute ” in SAS Component Objects:
Reference and the “ITEM_SIZE Attribute” in SAS Component Objects: Reference.
530 Chapter 22 Using DATA Step Component Objects
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.77.63