【问题标题】:SAS Macro to create multiple lags on longitudinal dataSAS 宏在纵向数据上创建多个滞后
【发布时间】:2017-05-04 13:41:04
【问题描述】:

我有一个不平衡的纵向数据集 Store_data:

Period    Store     Sales
 Jan        A         12
 Feb        A         10
 March      A         8
 April      A         3
 Jan        B         5
 Feb        B         19
 March      B         7
 April      B         8
 Jan        C         5
 Feb        C         19
 March      C         7
 April      C         8

目前,为了创建长达 2 年的销售滞后,我必须为每个订单手动创建滞后。即

data Store_lag;
    set Store_data;
    by Store;

    Sales_Lag1=lag(Sales);
    if first.Store then Sales_Lag1=.;

    Sales_Lag2=lag(Sales_Lag1);
    if first.Store then Sales_Lag2=.;

    *etc.....;
run;

我的问题是是否有一个宏来创建这些变量?当滞后订单数量变大时,它会变得特别乏味。

【问题讨论】:

  • 您有 SAS/ETS 许可(时间序列)吗?
  • 不。我知道我们可以在 ETS 中使用 proc expand,但我只安装了 SAS base/STAT。
  • 哇,滞后功能的有效用例! =)

标签: sas sas-macro


【解决方案1】:

数组处理在这里确实应该做得很好。这是一个例子。

data want;
  set have;
  by store;

  array lags[1:4] lags0-lags3;
  retain lags:;

  if first.store then 
     call missing(of lags[*]);    *clear out the array for each store;
  do _i = dim(lags) to 2 by -1;   *move the stack to the right;
    lags[_i] = lags[_i-1];
  end;
  lags[1] = sales;                *set the first one;
  drop lags0;                     *lags0 is the current sales, of course;
run;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-17
    • 1970-01-01
    相关资源
    最近更新 更多