SUMDUP Method

Retrieves the summary value for the current data item of the current key and stores the value in a DATA step variable.

Applies to: Hash object

Syntax

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, an appropriate error message is printed to the log.

object

specifies the name of the hash object.

SUM: variable-name

specifies a DATA step variable that stores the summary value for the current data item of the current key.

Details

You use the SUMDUP method to retrieve key summaries from the hash object when a key has multiple data items. For more information, see Maintaining Key Summaries in SAS Language Reference: Concepts.

Comparisons

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. The SUM method retrieves the summary value for a given key when only one data item exists per key.

Example: Retrieving a Summary Value

The following example uses the SUMDUP method to retrieve the summary value for the current data item. It also illustrates that it is possible to loop backward through the list by using the HAS_PREV and FIND_PREV methods. The FIND_PREV method works similarly to the FIND_NEXT method with respect to the current list item except that it moves backward through the multiple item list.
data dup;
   length key data 8;
   input key data;
   cards;
   1 10
   2 11
   1 15
   3 20
   2 16
   2 9
   3 100
   5 5
   1 5
   4 6
   5 99
;
data _null_;
   length r i sum 8;
   i = 0;
   dcl hash h(dataset:'dup', multidata: 'y', suminc: 'i');
   h.definekey('key');
   h.definedata('key', 'data');
   h.definedone();
   call missing (key, data);
   i = 1;
  do key = 1 to 5;
     rc = h.find();
     if (rc = 0) then do;
        h.has_next(result: r);
        do while(r ne 0);
           rc = h.find_next();
           rc = h.find_prev();
           rc = h.find_next();
           h.has_next(result: r);
        end;
     end;
  end;
 i = 0;
 do key = 1 to 5;
   rc = h.find();
   if (rc = 0) then do;
       h.sum(sum: sum);
       put key= data= sum=;
       h.has_next(result: r);
       do while(r ne 0);
          rc = h.find_next();
          h.sumdup(sum: sum);
          put 'dup ' key= data= sum=;
          h.has_next(result: r);
       end;
     end;
 end;
 run;
The following lines are written to the SAS log.
key=1 data=10 sum=2
dup key=1 data=15 sum=3
dup key=1 data=5 sum=2
key=2 data=11 sum=2
dup key=2 data=16 sum=3
dup key=2 data=9 sum=2
key=3 data=20 sum=2
dup key=3 data=100 sum=2
key=4 data=6 sum=1
key=5 data=5 sum=2
dup key=5 data=99 sum=2
To see how this works, consider the key 1, which has three data values: 10, 15, and 5 (which are stored in that order).
key=1 data=10 sum=2
dup key=1 data=15 sum=3
dup key=1 data=5 sum=2
When traversing the data list in the first do key = 1 to 5; loop, the key summary for data item 10 is set to 1 on the initial FIND method call. The first FIND_NEXT method call sets the key summary for data item 15 to 1. The next FIND_PREV method call moves back to data item 10 and increments its key summary to 2. Finally, the last call to the FIND_NEXT method increments the key summary for data item 15 to 2. The next iteration through the loop sets the key summary for data item 5 to 1 and the key summary for data item 15 to 3. Finally, the key summary for data item 5 is incremented to 2.
You do not call the HAS_PREV method before calling the FIND_PREV method in this example because you already know that there is a previous entry in the list. Otherwise, you would not be in the loop.
Also shown here is the necessity of using special methods for some duplicate operations. (In this case, the SUMDUP method works similarly to the SUM method by retrieving the key summary for the current data item.)

See Also

Non-Unique Key and Data Pairs in SAS Language Reference: Concepts
Methods: