SAS 宏在纵向数据上创建多个滞后

SAS Macro to create multiple lags on longitudinal data

我有一个不平衡的纵向数据集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;

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

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

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;