Writing a Hash Object to a Table

The ADD Method

The ADD method adds data in the PDV columns to the corresponding key and data components within a hash object. This method returns a zero if adding is successful and a nonzero if adding is not successful.
Syntax, ADD method:
object-name.ADD( )
object-name
specifies the name of the component object.
Tip:A zero value indicates success, and a nonzero value indicates failure.

Example: Adding Key and Data Values

The ADD method adds the key and data values to the hash object.
ContName.add(key:91, data:'North America');
ContName.add(key:93, data:'Europe');
ContName.add(key:94, data:'Africa');
ContName.add(key:95, data:'Asia');
ContName.add(key:96, data:'Australia/Pacific');

The OUTPUT Method

The OUTPUT method creates a table containing the data components of the hash object. The DATASET argument is used with the OUTPUT method to name the desired output table.
Syntax, OUTPUT method:
object-name.OUTPUT(DATASET:'data-set-name<(data-set-option)>' );
object-name
specifies the name of the component object.
DATASET:data-set-name
names the desired output table.
Tip:A zero value indicates success, and a nonzero value indicates failure.

Controlling Output with the OUTPUT Method

When you write a hash object to a table, you can specify how the output table should be sorted. Use the ORDERED argument in the DECLARE statement for the hash object, along with the OUTPUT method. The ORDERED argument specifies an order of ascending or descending for the key components.
(ORDERED:'option'<, ...>)
In the following example, when the ContName hash object is written to a table, the rows will be in descending order of the key components.
declare hash ContName (ordered:'descending');
Once you have read in all of your input data and added data to the hash object, you can write the hash object to a table. To determine when you have read in the last row of data, use the END= option. This option is applied in the SET statement and is not specific to hash objects. This option is set equal to a new column that you are creating. Note that this column is temporary, exists in the PDV, but is not in the final output table. SAS supplies for the column a value based on whether it is the end of file of the input table. A value of 0 means that it is not the last row of the input table, and a value of 1 means that it is the last row of the input table.
END=column
The following example sets the END= option to a new column, LastRow. Then, in a conditional statement, it checks to see whether LastRow is equal to 1 or the end of the file. If so, it writes the ContName hash object to an output table named Work.ContName.
set certadv.country(keep=ContinentID Country CountryName) end=lastrow;
if lastrow=1 then ContName.output(dataset: 'work.contname');

Avoiding Messages in the SAS Log

The hash object does not assign values to key variables, and the SAS compiler cannot detect the implicit key and data variable assignments that are handled by the hash object. Therefore, if no explicit assignment to a key or data variable appears in the program, SAS writes a note to the SAS log stating that the variables are uninitialized.
To avoid receiving these notes, use the CALL MISSING routine with the key and data variables as parameters. The CALL MISSING routine assigns a missing value to the specified character or numeric variables.
call missing(ContinentName);
Note: Another way to avoid receiving notes stating that the variables are uninitialized is to provide an initial assignment statement that assigns a missing value to each key and data variable.
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.144.227.251