For example, suppose you have a large data set that contains numeric lab results
corresponding to a unique patient number and weight. And suppose you have a small
data set that contains patient numbers (a subset of those in the large data set). You can
load the large data set into a hash object using the unique patient number as the key and
the weight values as the data. A single pass is made over the small data set using the
patient number to look up the current patient in the hash object whose weight is over a
certain value and output that data to a different data set.
Depending on the number of lookup keys and the size of the data set, the hash object
lookup can be significantly faster than a standard format lookup. If you’re just looking
up keys, you have a lot of memory, and you want fast performance, load the large data
set first. If you do not want to use a lot of memory, load the small data set first.
Declaring and Instantiating a Hash Object
You declare a hash object using the DECLARE statement. After you declare the new
hash object, use the _NEW_ operator to instantiate the object. For example:
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.
There is an alternative to the two-step process of using the DECLARE statement and the
_NEW_ operator to declare and instantiate a component object. You can use the
DECLARE statement to declare and instantiate the component object in one step.
declare hash myhash();
The above statement is equivalent to the following code:
declare hash myhash;
myhash = _new_ hash();
For more information, see “DECLARE Statement, Hash and Hash Iterator Objects” in
SAS Component Objects: Reference and the “_NEW_ Operator, Hash and Hash Iterator
Objects” in SAS Component Objects: Reference.
Initializing Hash Object Data Using a Constructor
When you create a hash object, you might want to provide initialization data. A
constructor is a method that you can use to instantiate a hash object and initialize the
hash object data.
The hash object constructor can have either of the following formats:
•
declare hash object_name(argument_tag-1: value-1
<, ...argument_tag-n: value-n>);
•
object_name = _new_ hash(argument_tag-1: value-1
<, ...argument_tag-n: value-n>);
For more information, see the “DECLARE Statement, Hash and Hash Iterator Objects”
in SAS Component Objects: Reference and the “_NEW_ Operator, Hash and Hash
Iterator Objects” in SAS Component Objects: Reference.
Using the Hash Object 519