declare hash myhash; myhash = _new_ hash();The DECLARE statement tells the compiler that the object reference MYHASH is of type hash. At this point, you have declared only the object reference MYHASH. It has the potential to hold a component object of type hash. You should declare the hash object only once. The _NEW_ operator creates an instance of the hash object and assigns it to the object reference MYHASH.
declare hash myhash();The above statement is equivalent to the following code:
declare hash myhash; myhash = _new_ hash();
length d $20;
length k $20;
if _N_ = 1 then do;
declare hash h();
rc = h.defineKey('k');
rc = h.defineData('d');
rc = h.defineDone();
end; You can have multiple key and data variables,
but the entire key must be unique. You can store more than one data
item with a particular key. For example, you could modify the previous
example to store auxiliary numeric values with the character key and
data. In this example, each key and each data item consists of a character
value and a numeric value.length d1 8;
length d2 $20;
length k1 $20;
length k2 8;
if _N_ = 1 then do;
declare hash h();
rc = h.defineKey('k1', 'k2');
rc = h.defineData('d1', 'd2');
rc = h.defineDone();
end;h.find(key:'abc')),
and the SAS compiler cannot detect the data variable assignments that
are performed by the hash object and the hash iterator. Therefore,
if no assignment to a key or data variable appears in the program,
SAS issues a note stating that the variable is uninitialized. To
avoid receiving these notes, you can perform one of the following
actions:
rc = h.find();
if (rc != 0) then
rc = h.add(); to a single method call:rc = h.ref();For more information, see the REF Method in SAS Component Objects: Reference.
Homer.data _null_;
length d $20;
length k $20;
/* Declare the 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();
end;
/* 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.';
/* Define constant value for key and data */
k = 'Joyce';
d = 'Ulysses';
/* 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.';
k = 'Homer';
/* Use the FIND method to retrieve the data associated with 'Homer' key */
rc = h.find();
if (rc = 0) then
put d=;
else
put 'Key Homer not found.';
run;The FIND method assigns the data value Odyssey,
which is associated with the key value Homer,
to the variable D.
data match;
length k 8;
length s 8;
if _N_ = 1 then do;
/* load SMALL data set into the hash object */
declare hash h(dataset: "work.small";
/* define SMALL data set variable K as key and S as value */
h.defineKey('k');
h.defineData('s');
h.defineDone();
/* avoid uninitialized variable notes */
call missing(k, s);
end;
/* use the SET statement to iterate over the LARGE data set using */
/* keys in the LARGE data set to match keys in the hash object */
set large;
rc = h.find();
if (rc = 0) then output;
run;The dataset argument
tag specifies the SMALL data set whose keys and data are read and
loaded by the hash object during the DEFINEDONE method. The FIND method
is then used to retrieve the data.
data _null_; length k count 8; length total 8; dcl hash myhash(suminc: 'count'); myhash.defineKey('k'); myhash.defineDone(); k = 99; count = 1; myhash.add(); /* COUNT is given the value 2.5 and the */ /* FIND sets the summary to 3.5*/ count = 2.5; myhash.find(); /* The COUNT of 3 is added to the FIND and */ /* sets the summary to 6.5. */ count = 3; myhash.find(); /* The COUNT of -1 sets the summary to 5.5. */ count = -1; myhash.find(); /* The SUM method gives the current value of */ /* the key summary to the variable TOTAL. */ myhash.sum(sum: total); /* The PUT statement prints total=5.5 in the log. */ put total=; run;
k = 99; count = 1; myhash.add(); /* key=99 summary is now 1 */ k = 100; myhash.add(); /* key=100 summary is now 1 */ k = 99; myhash.find(); /* key=99 summary is now 2 */ count = 2; myhash.find(); /* key=99 summary is now 4 */ k = 100; myhash.find(); /* key=100 summary is now 3 */ myhash.sum(sum: total); put 'total for key 100 = 'total; k = 99; myhash.sum(sum:total); put 'total for key 99 = ' total;
declare hash myhash(suminc: "keycount", dataset: "work.mydata");
data mydata; input key; datalines; 1 2 3 4 5 ; run; data keys; input key; datalines; 1 2 1 3 5 2 3 2 4 1 5 1 ; run; data count; length total key 8; keep key total; declare hash myhash(suminc: "count", dataset:"mydata"); myhash.defineKey('key'); myhash.defineDone(); count = 1; do while (not done); set keys end=done; rc = myhash.find(); end; done = 0; do while (not done); set mydata end=done; rc = myhash.sum(sum: total); output; end; stop; run;
Odyssey with Iliad,
and the REMOVE method deletes the entire data entry associated with
the Joyce key from the hash object.data _null_;
length d $20;
length k $20;
/* Declare the 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();
end;
/* Define constant value for key and data */
k = 'Joyce';
d = 'Ulysses';
/* 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.';
/* 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
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 */
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.rc = h.defineData('k1', 'k2', 'd1', 'd2');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);
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;