Previous Page | Next Page

Using DATA Step Component Objects

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, using 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.   [cautionend]

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;

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 will be returned in ascending key-value order.

For more information about the DECLARE statement and the _NEW_ operator, see the SAS Language Reference: Dictionary.


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 the SAS Language Reference: Dictionary.

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');
      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.

Messier Objects Greater than 12, Sorted by Right Ascension Values

       Messier Objects Greater than 12 Sorted by Right Ascension Values      

                            Obs      ra       obj

                              1    12 13.8    M98 
                              2    12 22.9    M100
                              3    12 29.8    M49 
                              4    12 39.5    M68 
                              5    12 42.0    M59 
                              6    13 29.9    M51 
                              7    13 42.2    M3  
                              8    16 41.7    M13 
                              9    16 57.1    M10 
                             10    17 37.6    M14 
                             11    17 56.8    M23 
                             12    18 20.8    M17 
                             13    18 31.6    M25 
                             14    18 36.4    M22 
                             15    18 53.6    M57 
                             16    19 53.8    M71 
                             17    20 23.9    M29 
                             18    21 32.2    M39 

Previous Page | Next Page | Top of Page