Like Tom said, sql will be a lot easier in this case and, in fact, can do everything in just a couple of lines. Here is the sql code and yet another way to do it in a datastep: proc sql; create table want as select *,a_2/sum(a_2) as f_5 from have group by unitid,year order by year,unitid,code ; quit; /*or*/ proc sort data=have; by unitid year code; run; data want (drop=sum_f_5); do until (last.year); set have (where=(code ne 0)); by unitid year; sum_f_5=ifn(first.year,a_2,sum(sum_f_5,a_2)); end; do until (last.year); set have; by unitid year; f_5=a_2/sum_f_5; output; end; run; proc sort data=want; by year unitid code; run;
... View more