Using the Hash Iterator Object
About the Hash Iterator Object
Use the hash iterator object to store and search data based on lookup keys. The hash
iterator object enables you to retrieve the hash object data in either forward or reverse
key order.
Declaring and Instantiating a Hash Iterator Object
You declare a hash iterator object by using the DECLARE statement. After you declare
the new hash iterator object, use the _NEW_ operator to instantiate the object. Use the
hash object name as an argument tag. For example:
declare hiter myiter;
myiter = _new_ hiter('h');
The DECLARE statement tells the compiler that the object reference MyIter is of type
hash iterator. At this point, you have declared only the object reference MyIter. It has the
potential to hold a component object of type hash iterator. You should declare the hash
iterator object only once. The _NEW_ operator creates an instance of the hash iterator
object and assigns it to the object reference MyIter. The hash object, H, is passed as a
constructor argument. The hash object, not the hash object variable, is specifically
assigned to the hash iterator.
As 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 declare and
instantiate a hash iterator object in one step by using the DECLARE statement as a
constructor method. The syntax is as follows:
declare hiter object_name(hash_object_name);
In the above example, the hash object name must be enclosed in single or double
quotation marks.
For example:
declare hiter myiter('h');
The previous statement is equivalent to these:
declare hiter myiter;
myiter = _new_ hiter('h');
Note: You must declare and instantiate a hash object before you create a hash iterator
object. For more information, see “Declaring and Instantiating a Hash Object” on
page 529.
For example:
if _N_ = 1 then do;
length key $10;
declare hash myhash(dataset:"work.x", ordered: 'yes');
declare hiter myiter('myhash');
myhash.defineKey('key');
myhash.defineDone();
end;
Using the Hash Iterator Object 541
This code creates an instance of a hash iterator object with the variable name MyIter.
The hash object, MyHash, is passed as the constructor argument. Because the hash
object was created with the ORDERED argument tag set to 'yes', the data is returned
in ascending key-value order.
For more information about the DECLARE statement and the _NEW_ operator, see the
SAS DATA Step Statements: Reference.
Example: Retrieving Hash Object Data By Using the Hash Iterator
Using the data set ASTRO that contains astronomical data, the following code creates
the data set that contains Messier objects (OBJ) whose right-ascension (RA) values are
greater than 12. The FIRST and NEXT methods are used to retrieve the data in
ascending order. For more information about the FIRST and NEXT methods, see SAS
Component Objects: Reference.
data astro;
input obj $1-4 ra $6-12 dec $14-19;
datalines;
M31 00 42.7 +41 16
M71 19 53.8 +18 47
M51 13 29.9 +47 12
M98 12 13.8 +14 54
M13 16 41.7 +36 28
M39 21 32.2 +48 26
M81 09 55.6 +69 04
M100 12 22.9 +15 49
M41 06 46.0 -20 44
M44 08 40.1 +19 59
M10 16 57.1 -04 06
M57 18 53.6 +33 02
M3 13 42.2 +28 23
M22 18 36.4 -23 54
M23 17 56.8 -19 01
M49 12 29.8 +08 00
M68 12 39.5 -26 45
M17 18 20.8 -16 11
M14 17 37.6 -03 15
M29 20 23.9 +38 32
M34 02 42.0 +42 47
M82 09 55.8 +69 41
M59 12 42.0 +11 39
M74 01 36.7 +15 47
M25 18 31.6 -19 15
;
run;
data out;
if _N_ = 1 then do;
length obj $10;
length ra $10;
length dec $10;
/* Read ASTRO data set and store in asc order in hash obj */
declare hash h(dataset:"work.astro", ordered: 'yes');
/* Define variables RA and OBJ as key and data for hash object */
declare hiter iter('h');
542 Chapter 24 Using DATA Step Component Objects
h.defineKey('ra');
h.defineData('ra', 'obj');
h.defineDone();
/* Avoid uninitialized variable notes */
call missing(obj, ra, dec);
end;
/* Retrieve RA values in ascending order */
rc = iter.first();
do while (rc = 0);
/* Find hash object keys greater than 12 and output data */
if ra GE '12' then
output;
rc = iter.next();
end;
run;
proc print data=work.out;
var ra obj;
title 'Messier Objects Greater than 12 Sorted by Right Ascension Values';
run;
The following output shows the report that PROC PRINT generates.
Using the Hash Iterator Object 543

Get SAS 9.4 Language Reference, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.