If your system has constrained resources you can use a hash to maintain a 'group-wise pivot summary' that is output upon completion of a DOW loop over the group.
data want2;
if 0 then set have; * prep pdv (column order and types);
if _n_ = 1 then do;
length index 8 name value $32 metric_sum 8.;
declare hash smy(ordered:'a');
smy.defineKey ('index', 'name', 'value');
smy.defineData('index', 'name', 'value', 'metric_sum'); * track pivoted categoricals and aggregate;
smy.defineDone();
call missing(name, value, metric_sum);
declare hiter hi('smy');
end;
do until (last.id2); * DOW loop;
set have;
by id id2 notsorted; * process groups contiguous wise;
array vars var1-var100;
do index=1 to dim(vars);
name = vname(vars(index)); /* pivot categoricals */
value = vars(index);
if smy.find() = 0 /* compute aggregate */
then metric_sum + metric;
else metric_sum = metric;
smy.replace();
end;
end;
do while(hi.next() = 0);
output; /* output each pivot item within group */
end;
smy.clear(); /* reset pivot aggregate tracking */
keep id id2 index name value metric_sum;
run;
... View more