2017-05-04 99 views
1

我具有不平衡的縱向數據集Store_data創建多個滯後:SAS宏以縱向數據

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; 

我的問題是如果有一個宏來創建這樣的變量?當滯後階數變大時,它變得特別乏味。

+0

您是否擁有SAS/ETS授權(時間系列)? – Joe

+0

沒有。我知道我們可以在ETS中使用proc擴展,但我只安裝了SAS基本/ STAT。 – user90831

+0

哇功能的有效用例! =) –

回答

3

陣列處理真的應該在這裏做得很好。這是一個例子。

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;