SUM Method

Retrieves the summary value for a given key from the hash table and stores the value in a DATA step variable.

Applies to: Hash object

Syntax

Required Arguments

rc

specifies whether the method succeeded or failed.

A return code of zero indicates success; a nonzero value indicates failure. If you do not supply a return code variable for the method call and the method fails, then an appropriate error message is written to the log.

object

specifies the name of the hash object.

KEY: keyvalue

specifies the key value whose type must match the corresponding key variable that is specified in a DEFINEKEY method call.

The number of “KEY: keyvalue” pairs depends on the number of key variables that you define by using the DEFINEKEY method.

SUM: variable-name

specifies a DATA step variable that stores the current summary value of a given key.

Details

You use the SUM method to retrieve key summaries from the hash object. For more information, see Maintaining Key Summaries in SAS Language Reference: Concepts.

Comparisons

The SUM method retrieves the summary value for a given key when only one data item exists per key. The SUMDUP method retrieves the summary value for the current data item of the current key when more than one data item exists for a key.

Example: Retrieving the Key Summary for a Given Key

The following example uses the SUM method to retrieve the key summary for each given key, K=99 and K=100.
 k = 99;
 count = 1;
 h.add();
 /* key=99 summary is now 1 */
 k = 100;
 h.add();
 /* key=100 summary is now 1 */
 k = 99;
 h.find();
 /* key=99 summary is now 2 */
 count = 2;
 h.find();
 /* key=99 summary is now 4 */
 k = 100;
 h.find();
 /* key=100 summary is now 3 */
 h.sum(sum: total);
 put 'total for key 100 = ' total;
 k = 99;
 h.sum(sum:total);
 put 'total for key 99 = ' total;
run;
The first PUT statement prints the summary for k=100:
total for key 100 = 3
The second PUT statement prints the summary for k=99:
total for key 99 = 4