Using the Hash Iterator Object


Introducing the Hash Iterator Object

Use the hash object to store and search data based on lookup keys. The hash iterator object enables you to retrieve the hash object data in 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_ statement 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 variable MYITER is of type hash iterator. At this point, you have only declared the variable 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_ statement creates an instance of the hash iterator object and assigns it to the variable MYITER. The hash object, H, is passed as a constructor argument.

As an alternative to the two-step process of using the DECLARE and the _NEW_ statements 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  variable_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 438.

For example:

 if _N_ = 1 then do;     length key ;     declare hash myhash(hashexp: 4, 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_ Statement', see 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 (OBJ) objects whose right-ascension (RA) values are greater than 12. The FIRST and NEXT methods are used to sort the data in ascending order. The FIRST and NEXT methods are used to sort the data. For more information about the 'FIRST Method' and the ' NEXT Method', see SAS Language Reference: Dictionary .

 data astro;     input obj   4 ra   12 dec   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 ;        length ra ;        length dec ;        /* Read ASTRO data set as ordered */        declare hash h(hashexp: 4, 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;  /* Sort hash object by right ascension values */  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.

Output 24.2: Messier Objects Greater than 12, Sorted by Right Ascension Values
start example
 Messier Objects Greater than 12 Sorted by Right Ascension Values     1                       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 
end example
 



SAS 9.1 Language Reference. Concepts
SAS 9.1 Language Reference Concepts
ISBN: 1590471989
EAN: 2147483647
Year: 2004
Pages: 255

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net