2017-01-09 130 views
0

我一直在尋找解決方案,但在社區中我找不到SAS中的任何類似問題。SAS中的條件累計總和

我在SAS中有下表。我需要通過日期獲得累計值。最重要的是,我需要累計值從新開始,一旦超過。

Date   Number 
01/01/2017  3 
01/01/2017  1 
01/01/2017  3 
01/01/2017  4 
01/01/2017  6 
01/01/2017  8 
02/01/2017  6 
02/01/2017  3 
02/01/2017  5 
02/01/2017  7 
03/01/2017  4 
03/01/2017  3 
    ...   ... 

我需要我的輸出表看起來像這樣。再多一欄顯示累計值。

Date   Number  cumulative 
01/01/2017  3   3 
01/01/2017  1   4   
01/01/2017  3   7 
01/01/2017  4   4    <---- (starts from new) 
01/01/2017  6   10 
01/01/2017  8   8 
02/01/2017  6   6 
02/01/2017  3   9 
02/01/2017  5   5 
02/01/2017  7   7    <---- (starts from new) 
03/01/2017  4   3 
03/01/2017  3   7 
    ...   ...   ... 

有人能夠協助這一點。

感謝

+0

請出示你已經嘗試什麼樣的代碼。您是否在RETAIN中使用DATA步驟?此外,看起來像一個錯字,倒數第二行的累積數應該是4,而不是3. – Quentin

+0

如果您發佈了更好的解釋,那麼您一定會得到答案。如果你更好地定義規則,這絕對是一個人會幫助你的難題。 –

回答

1

喜歡的東西(未經測試):

data out; 
    set in; 
    by date; * Assumes sorted by date; 
    retain cumulative; 
    if first.date or cumulative+number > 10 then do; 
    cumulative = 0; 
    end; 
    cumulative = cumulative + number; 
run; 

應該工作...