每周时间滞后

问题描述 投票:0回答:1

假设有以下数据集:

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周?

提前谢谢您

sas
1个回答
0
投票

使用

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 周的值。

© www.soinside.com 2019 - 2024. All rights reserved.