假设有以下数据集:
data dataset;
input week_number Var1 Var2;
cards;
1 55 98
2 34 34
3 21 0
4 76 12
5 98 0
.. ... ..
52 21 43
;run;
计算和添加以下内容的最佳方法是什么:滞后-1周;滞后+1周;滞后-2周;比原始数据集滞后+2周?
提前谢谢您
使用
proc sql
,您可以多次连接数据本身,并使用连接条件控制每行中出现的周:
proc sql;
create table want as
select have.*
,lag1.Var1 as Var1_lag1
,lag1.Var2 as Var2_lag1
,lag2.Var1 as Var1_lag2
,lag2.Var2 as Var2_lag2
,lead1.Var1 as Var1_lead1
,lead1.Var2 as Var2_lead1
,lead2.Var1 as Var1_lead2
,lead2.Var2 as Var2_lead2
from dataset have left join dataset lag1 on lag1.Week_number = have.week_number - 1
left join dataset lag2 on lag2.week_number = have.week_number - 2
left join dataset lead1 on lead1.week_number = have.week_number + 1
left join dataset lead2 on lead2.week_number = have.week_number + 2
;
quit;
带有
_lag1
的变量具有延迟 1 周、_lag2
延迟 2 周、_lead1
提前 1 周和 _lead2
提前 2 周的值。