Defining Hash Objects

Hash Object Methods

Hash object methods are operations that are performed on a hash object using dot notation. With dot notation, specify the name of the hash object to the left of the dot and specify the method to the right of the dot. All methods include a set of parentheses. Within the parentheses, arguments can be specified.
Table 12.2 Hash Object Methods
Method
Description
Syntax
ADD
Adds the specified data that is associated with the given key to the hash object.
object-name.ADD( )
DEFINEDATA
Defines data to be stored in the hash object.
object-name.DEFINEDATA( )
DEFINEDONE
Specifies that all key and data definitions are complete.
object-name.DEFINEDONE( )
DEFINEKEY
Defines key components to the hash object.
object-name.DEFINEKEY( )
FIND
Determines whether the key is stored in the hash object.
object-name.FIND( )
OUTPUT
Creates one or more data sets containing the data in the hash object.
object-name.OUTPUT( )

Defining a Hash Object

To define a hash object, three DEFINE methods are required.
object-name.DEFINEKEY('key-1' <, ...'key-n '>)
object-name.DEFINEDATA('data-1' <, ...'data-n '>)
object-name.DEFINEDONE( );
  • The DEFINEKEY method defines the columns that make up the key component. A column can be specified as numeric or character. A character column can be literal in quotation marks, a character column, or a character expression.
  • The DEFINEDATA method defines the columns that make up the data component. A column can be specified as a character literal in quotation marks, a character column, or a character expression.
  • The DEFINEDONE method indicates that all key and data components are complete. This method also loads the hash object if a table is specified in the DECLARE statement.
Tip
Multiple DEFINEKEY and DEFINEDATA statements can be used for one hash object.

Example: Defining a Hash Object

The following example defines the hash object Airports.
data work.report;
      if 0 then
         set certadv.ctcities (keep=Code City Name);
   if _N_=1 then do;                                      /*1*/
   declare hash airports (dataset: "certadv.ctcities");   /*2*/
   airports.definekey ("Code");                           /*3*/
   airports.definedata ("City", "Name");                  /*4*/
   airports.definedone();                                 /*5*/
end;
1 When _N_ is equal to 1, the code declares and defines the hash object only for the first DATA step iteration. This statement is never executed, but it makes a place in the PDV for every column that is in the table. Without this logic, the statements that are associated with the hash object are all executable. Hash object memory is not released and reused each time. You can potentially run out of memory if you have a lot of data to load into the hash object.
2 The DECLARE statement declares a hash object named Airports. The DATASET argument refers to the Certadv.CtCities data set that contains the values to be loaded.
3 The DEFINEKEY method defines Code as the key component.
4 The DEFINEDATA method defines City and Name as the data components.
5 The DEFINEDONE method loads the Certadv.CtCities data set into the hash object.
Table 12.3 Hash Object Airports
Key: Code
Data: City
Data: Name
ANC
Anchorage, AK
Anchorage International Airport
BNA
Nashville, TN
Nashville International Airport
CDG
Paris
Charles de Gaulle
LAX
Los Angeles, CA
Los Angeles International Airport
RDU
Raleigh-Durham, NC
Raleigh-Durham International Airport
Note: The hash object can store multiple key variables as well as multiple data variables.
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.138.124.143