It uses same logic as before. You just need retain one more total and divide totals at last:
data want;
set have;
by id group;
retain tot_sum_mult tot_sum_x;
if first.group then do;
tot_sum_x = x;
tot_sum_mult = x * y;
end; else do;
tot_sum_x = sum(of tot_sum_x, x);
tot_sum_mult = sum(of tot_sum_mult, x * y);
end;
if last.group then do;
result = tot_sum_mult / tot_sum_x;
output;
end;
drop tot_sum_mult tot_sum_x;
run;
... View more