Using Hash Iterator Objects

What Is a Hash Iterator Object?

A hash iterator object is associated with a hash object, enabling you to retrieve hash object data in either forward or reverse key order. Think of the hash iterator object, or hiter, as an ordered view of a hash object.
Figure 12.4 Hash Object Iterator
Hash Object Iterator
Note: Declare the hash object before defining the hash iterator object.
In the following example, the Certadv.Orderfact table is loaded into the hash object Customer. The hash object Customer then feeds into the hash iterator object C. Notice that there is a DECLARE statement for the hash object Customer and a DECLARE statement for hiter (hash iterator) object C. The hash object name must be enclosed in quotation marks when it is referenced in the DECLARE statement for the hash iterator object.
declare hash Customer (dataset: 'certadv.orderfact', ordered: 'descending');
...statements..
declare hiter C('Customer');
The ORDERED argument in the hash object is not controlling the sort order of the hash object. The ORDERED argument controls the sort order of an output table only if an OUTPUT method is used in the hash object or the sort order of a hash iterator object that points to the hash object.

Declaring and Defining a Hash Object and a Hash Iterator Object

The hash object loads the data in the order in which it occurs in the data set. The ORDERED argument specifies retrieving data from the hash object by descending order of key values.
declare hash Customer(dataset: 'certadv.orderfact', ordered: 'descending');
The DEFINEKEY, DEFINEDATA, and DEFINEDONE methods execute. The key values are not returned to the PDV.
customer.definekey('TotalRetailPrice', 'CustomerID');
customer.definedata('TotalRetailPrice', 'CustomerID', 'ProductID');
customer.definedone();
The second DECLARE statement defines the hash iterator object, C. It points to the hash object, Customer, whose data is in descending order of key values.
declare hiter C('customer');

Retrieving Hash Object Data with the Hash Iterator Object

Hash iterator methods are operations that are performed on a hash iterator object using dot notation. The FIRST and NEXT methods read data in forward key order. The LAST and PREV methods read data in reverse key order.
Table 12.4 Hash Iterator Methods
Method
Description
Syntax
Example
FIRST
Returns the first data value in the underlying hash object.
object-name.FIRST( );
declare hiter C('customer');
<more SAS code>;
C.first();
LAST
Returns the last data value in the underlying hash object.
object-name.LAST( );
declare hiter C('customer');
<more SAS code>;
C.last();
NEXT
Returns the data components in key order.
Note: If you use the NEXT method without the FIRST method, it returns the first item in the hash object.
object-name.NEXT( );
C.first();
<more SAS code>;
C.next();
C.next();
PREV
Returns the data components in reverse key order.
object-name.PREV( );
C.prev();
object-name
specifies the name of the hash iterator object.
  • The FIRST method returns the first data item in the hash object. The LAST method returns the last data component in the hash object.
  • You can use the NEXT method to return the next data component in the hash object.
  • Use the PREV method iteratively to traverse the hash object and return the data components in reverse key order.
Note: The PREV method sets the data variable to the value of the data component so that it is available for use after the method call.

Example: Using the Hash Iterator Object

Suppose you need to identify which two customers ordered the most and least expensive items. You can use the hash iterator object to retrieve the data in either ascending or descending key order to efficiently identify these four customers.
data work.topbottom;
   drop i;
   if _N_=1 then do;
      if 0 then set certadv.Orderfact (keep=CustomerID 
                                      ProductID TotalRetailPrice);
      declare hash customer(dataset: 'certadv.Orderfact',
                            ordered: 'descending');
      customer.definekey('TotalRetailPrice', 'CustomerID');
      customer.definedata('TotalRetailPrice', 'CustomerID',
                          'ProductID');
      customer.definedone();
      declare hiter C('customer');
end;
   C.first();
   do i=1 to 2;
      output work.topbottom;
      C.next();
   end;
   C.last();
   do i=1 to 2;
      output work.topbottom;
      C.prev();
   end;
   stop;
run;
Output 12.2 PROC PRINT Output of Work.TopBottom
PROC PRINT Output of Work.TopBottom
Last updated: October 16, 2019
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.203.215