You can maintain a summary
count for a hash object key by using the SUMINC argument tag when
you declare the hash object. The tag value is a string expression
that resolves to the name of a numeric DATA step variable –
the SUMINC variable.
This SUMINC tag instructs
the hash object to allocate internal storage for maintaining a summary
value for each key.
The summary value of
a hash key is initialized to the value of the SUMINC variable whenever
the ADD or REPLACE method is used.
The summary value of
a hash key is incremented by the value of the SUMINC variable whenever
the FIND, CHECK, or REF method is used.
Note that the SUMINC
variable can be negative, positive, or zero valued. The variable does
not need to be an integer. The SUMINC value for a key is zero by default.
In the following example,
the initial ADD method sets the summary count for K=99 to 1 before
the ADD. Then each time a new COUNT value is given, the following
FIND method adds the value to the key summary. In this example, one
data value exists for each key. The SUM method retrieves the current
value of the key summary and the value is stored in the DATA step
variable TOTAL. If multiple items exist for each key, the SUMDUP method
retrieves the current value of the key summary.
data _null_;
length k count 8;
length total 8;
dcl hash myhash(suminc: 'count');
myhash.defineKey('k');
myhash.defineDone();
k = 99;
count = 1;
myhash.add();
/* COUNT is given the value 2.5 and the */
/* FIND sets the summary to 3.5*/
count = 2.5;
myhash.find();
/* The COUNT of 3 is added to the FIND and */
/* sets the summary to 6.5. */
count = 3;
myhash.find();
/* The COUNT of -1 sets the summary to 5.5. */
count = -1;
myhash.find();
/* The SUM method gives the current value of */
/* the key summary to the variable TOTAL. */
myhash.sum(sum: total);
/* The PUT statement prints total=5.5 in the log. */
put total=;
run;
In this example, a summary
is maintained for each key value K=99 and K=100:
k = 99;
count = 1;
myhash.add();
/* key=99 summary is now 1 */
k = 100;
myhash.add();
/* key=100 summary is now 1 */
k = 99;
myhash.find();
/* key=99 summary is now 2 */
count = 2;
myhash.find();
/* key=99 summary is now 4 */
k = 100;
myhash.find();
/* key=100 summary is now 3 */
myhash.sum(sum: total);
put 'total for key 100 = 'total;
k = 99;
myhash.sum(sum:total);
put 'total for key 99 = ' total;
The first PUT statement
prints the summary for K=100:
total for key 100 = 3
And the second PUT statement
prints the summary for K=99:
total for key 99 = 4
You can use key summaries
in conjunction with the
dataset argument
tag. As the data set is read into the hash object using the DEFINEDONE
method, all key summaries are set to the SUMINC value. And, all subsequent
FIND, CHECK, or ADD methods change the corresponding key summaries.
declare hash myhash(suminc: "keycount", dataset: "work.mydata");
You can use key summaries
for counting the number of occurrences of given keys. In the following
example, the data set MYDATA is loaded into a hash object and uses
key summaries to keep count of the number of occurrences for each
key in the data set KEYS. (The SUMINC variable is not set to a value,
so the default initial value of zero is used.)
data mydata;
input key;
datalines;
1
2
3
4
5
;
run;
data keys;
input key;
datalines;
1
2
1
3
5
2
3
2
4
1
5
1
;
run;
data count;
length total key 8;
keep key total;
declare hash myhash(suminc: "count", dataset:"mydata");
myhash.defineKey('key');
myhash.defineDone();
count = 1;
do while (not done);
set keys end=done;
rc = myhash.find();
end;
done = 0;
do while (not done);
set mydata end=done;
rc = myhash.sum(sum: total);
output;
end;
stop;
run;
Here is the output for
the resulting data set.
For more information,
see the SUM Method in SAS Component Objects: Reference and the SUMDUP Method in SAS Component Objects: Reference.