Declaring Hash Objects

A Brief Overview

Hash objects are helpful when performing table lookups. A hash object is an in-memory table that contains key and data components. In the following example, the hash object contains two key columns, col_A and col_B, and three data columns, col_C, col_D, and col_E.
Figure 12.1 Hash Object with Key and Data Components
Hash Object With Key and Data Components
You can quickly and efficiently store, search, and retrieve data based on the key components. When the key component values are found, the data values are copied to the program data vector (PDV).
The use of hash objects is specific to the DATA step. A hash object is available only to the DATA step that creates it. When the DATA step is over, the hash object is deleted. Whereas an array is defined at compile time, a hash object is defined at execution. This makes the size of a hash object dynamic, and that means that the object can expand and shrink as data is added or deleted.
A hash object resembles a table with rows and columns and contains key and data components.

Key Components

  • Key components can consist of one or more key columns.
  • Key components can be numeric, character, or both.
  • Key components must be defined as PDV columns, and your hash object does not have to be sorted by the keys.
  • Be default, each row of keys must be unique.

Data Components

  • You can have multiple data components per each row of key components.
  • Data components can be numeric, character, or both.
  • Data components must be defined as PDV columns.

Declaring a Hash Object

The DECLARE statement creates an instance of data, and initializes data for an object. Use the DECLARE statement to name your hash object and specify information such as the name of the table that should be used to load the hash object.
Syntax, DECLARE statement:
DECLARE object object-name(<argument_tag-1:value-1, ...>);
object
specifies the component object. An object can be one of the following values:
hash
indicates a hash object.
hiter
indicates a hash iterator object.
object-name
specifies the name of the hash object.
argument_tag-1
specifies the information that is used to create an instance of the component object.
DATASET: 'data-set-name <(data-set-option)>'
value
specifies the value for an argument tag. Valid values depend on the component object.
In the following example, you are creating a hash object named States. The example does not contain any arguments or tag values. The parentheses are still required.
declare hash States();
In the following example, you are reading the data for the hash object from a table. In the example, the DATASET argument loads the Work.Population_USStates table into the hash object. After the argument DATASET, place a colon and then the value of the argument. If you specify a literal value for the table, enclose the value in single or double quotation marks. Within those quotation marks and after the table name, specify data set options such as WHERE=, DROP=, KEEP=, RENAME=, or OBS=.
declare hash States(dataset:'work.population_usstates
                    (where=(StatePop2017>20000000))');
If you have duplicate key values, use the MULTIDATA argument in the DECLARE statement. If you want to allow duplicate key components, then set MULTIDATA to 'YES'. The default value is 'NO'.
declare hash ContName(MULTIDATA:'YES');

Hash Object Process

Besides declaring the hash object, you must also provide key and data components by using the three DEFINE methods. Once the hash object is declared and defined, the hash object can be used. Finding values in a hash object is only one of the ways that you can use hash objects. Other ways include adding data to the hash object and writing data from the hash object to an output table.
Table 12.1 Hash Object Process
Steps
Syntax
Example
Declare the hash object.
DECLARE object object-name
(<argument_tag-1:value-1, ...>);
declare hash ContName();
Define the hash object.
object-name.DEFINEKEY('key-1' <, ...'key-n'>);
ContName.definekey('ContinentID');
object-name.DEFINEDATA('data-1' <, ...'data-n'>);
ContName.definedata('ContinentName');
object-name.DEFINEDONE( );
ContName.definedone();
Use the hash object.
object-name.FIND(<KEY:value-1, ... KEY:value-n>);
rc=ContName.find();
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
18.226.180.16