聽起來像「滾動12個月總和」。如果是這樣,使用不同的數據結構(不是2個變量,而是24行1變量)要容易得多。那麼您就擁有了所有的ETS工具,或者是SQL或SAS數據步驟中的簡單流程。
如果你不能/不會重構你的數據,那麼你可以通過將數據加載到臨時數組(或散列表,但對於新手來說數組更簡單)來做到這一點。這使您可以直接訪問整個事情。例如:
data have;
do month = 1 to 12;
q_2014 = rand('Uniform')*10+500+month*5;
q_2015 = rand('Uniform')*10+550+month*5;
output;
end;
run;
data want;
array q2014[12] _temporary_; *temporary array to hold data;
array q2015[12] _temporary_;
if _n_=1 then do; *load data into temporary arrays;
do _n = 1 to n_data;
set have point=_n nobs=n_data;
q2014[_n] = q_2014;
q2015[_n] = q_2015;
end;
end;
set have;
do _i = 1 to _n_; *grab the this_year data;
q_rolling12 = sum(q_rolling12,q2015[_i]);
end;
do _j = _n_+1 to n_data;
q_rolling12 = sum(q_rolling12,q2014[_j]);*grab the last_year data;
end;
run;
來源
2016-07-08 17:18:55
Joe