2017-01-11 91 views
1

我的問題是關於SAS中的條件累計金額。我認爲使用樣本可以更好地解釋這一點。我有以下數據集:SAS_Conditional累計金額

Date     Value 
01/01/2001   10 
02/01/2001   20 
03/01/2001   30 
04/01/2001   15 
05/01/2001   25 
06/01/2001   35 
07/01/2001   20 
08/01/2001   45 
09/01/2001   35 

我想查找值的累積和。我的情況是如果累計總和超過70,應該是70,下累計總和應來自70過多的價值就開始等等。更preciesly,我的新的數據應該是:

Date     Value Cumulative 
01/01/2001   10  10 
02/01/2001   20  30 
03/01/2001   30  60 
04/01/2001   15  70 
05/01/2001   25  30 (75-70=5+25=30) 
06/01/2001   35  65 
07/01/2001   20  70 
08/01/2001   45  60 (85-70=15+45=60) 
09/01/2001   35  95 (because its last value) 

許多在此先感謝

回答

1

這是一個解決方案,但必然會有一個更優雅。它與if eof分成兩部分以滿足最後的觀察條件。

data want; 
    set test end = eof; 

    if eof ^= 1 then do; 

    if cumulative = 70 then cumulative = extra; 

    Cumulative + value; 
    extra = cumulative - 70; 

    if extra > 0 then do; 
     cumulative = 70; 
    end; 
    end; 

    retain extra; 
    retain cumulative; 

    if eof = 1 then cumulative + value; 

run; 
+0

有可能是一個更優雅的,但我真的很喜歡你的。它還處理OP未提及的條件,例如> 70的剩餘部分,所以額外需要被推到多行(可能遠至最後一行)。 – vknowles

+0

非常感謝。 – Khalid