This is the updated version and I hope it meets what you need. For the existing code, it is easier just to add another array. data time1; input firm_id $6. date yymmdd10. return; format date yymm7.; cards; firmA 2012-11-01 . firmA 2013-01-01 100 firmA 2013-02-01 100 firmA 2013-03-01 100 firmA 2013-08-01 100 firmA 2013-09-01 100 firmA 2013-10-01 100 firmB 2012-11-01 . firmB 2013-01-01 100 firmB 2013-02-01 100 firmB 2013-03-01 100 firmB 2013-04-01 100 firmB 2013-05-01 100 firmB 2013-07-01 100 firmB 2013-08-01 100 firmB 2013-09-01 100 firmB 2013-10-01 100 ;;;; %let mon=3; data want; array sum_rol(0:%eval(&mon.-1)) _temporary_; array mth (0:%eval(&mon.-1)) _temporary_; do _n_=1 by 1 until (last.firm_id); set time1; by firm_id; sum_rol(mod(_n_,&mon.))= return; mth (mod(_n_,&mon.))= date; if n(of sum_rol(*))=&mon. and intck('month', min(of mth(*)), max(of mth(*))) = 2 then rolling_sum=sum(of sum_rol(*)); else rolling_sum=.; output; end; call missing (of sum_rol(*)); call missing (of mth(*)); run;
... View more