```Date: Mon, 1 Dec 2008 19:55:09 -0500 Reply-To: kwu0914 Sender: "SAS(r) Discussion" From: kwu0914 Organization: Aioe.org NNTP Server Subject: Re: Past n days average price Comments: To: sas-l@uga.edu I should make the question more clear: Day 1 is the most recently day, such as input data set is in following format: day price 12-01-2008 10 11-30-2008 12 11-29-2008 11 11-28-2008 14 11-27-2008 16 11-26-2008 17 so the computing average has to be performed forward, not backward. Also if n = 200, 200 LAG statements might be noisy. "Mary" wrote in message news:073401c9540d\$d00a6520\$832fa8c0@HP82083701405... > Yes. You can use the lag functions, but be careful about not putting them > in IF statements. > > data test; > infile cards; > input day price; > lag1price= lag1(price); > lag2price= lag2(price); > lag3price= lag3(price); > movingaverage= (price + lag1price + lag2price + lag3price) /4; > cards; > 1 10 > 2 12 > 3 11 > 4 14 > 5 16 > 6 17 > run; > > -Mary > > > "kwu0914" wrote in message > news:gh1rhm\$ts5\$1@aioe.org... >> Hello all SAS_L, here is a data set A: >> >> day price >> 1 10 >> 2 12 >> 3 11 >> 4 14 >> 5 16 >> 6 17 >> ....... >> >> my question is how to compute past n days average? For example, >> if n = 3 then the desired output will be: >> >> day price day3_avg >> 1 10 11 <--- (10+12+11)/3 >> 2 12 12.3 <--- (12+11+14)/3 >> 3 11 13.7 <--- (11+14+16)/3 >> 4 14 15.7 <--- (14+16+17)/3 >> 5 16 ...... >> 6 17 ...... >> >> Any suggestions? Few words of idea is enough!! Seems problem can not be >> solved >> in one pass, if this is the case, what the minimum # of pass does program >> really need? >> Is it possibile to run less than in O(N) time? Thanks! Kevin ```

Back to: Top of message | Previous page | Main SAS-L page