Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: Average of previous years

 From Charles Koss <[email protected]> To [email protected] Subject Re: st: Average of previous years Date Mon, 11 Apr 2011 08:48:26 -0500

```There are two other ways for obtaining the same results, by using the
tsmooth command. In the first case, you need to place dots while in
the second case you need an auxiliary series. In conclusion, for the
problem at hand, Dr. Cox's code is the fastest way to go. However, at
greater windows (say 20) is ineficient. Take a look at the following
code, I think the second case is the least prone to error.

//START
clear
webuse bsales
line sales t
generate float salesm5= (l.sales+l2.sales+l3.sales+l4.sales+l5.sales)/5

//First CASE
tssmooth ma sm2 = l5.sales, window(0 1 4)
//replace values in obs 2 to 5
replace sm2 = . in 2
replace sm2 = . in 3
replace sm2 = . in 4
replace sm2 = . in 5

//Second CASE
//aux ser =sm1
tssmooth ma sm1 = sales, window(0 1 4)
generate float salesm5ma = l5.sm1
//END

Charles

--
Charles Koss
http://charlesonnet.blogspot.com

On Sat, Apr 9, 2011 at 1:02 PM, Nick Cox <[email protected]> wrote:
> gen y = (L1.x + L2.x + L3.x + L4.x + L5.x) / 5
>
> !!!
>
> On Sat, Apr 9, 2011 at 5:19 PM, Nick Cox <[email protected]> wrote:
>> Suppose you have -tsset- your data. Then this is
>>
>> gen y = (L1.x + L2.x + L3.x + L4.x + L5.x)
>>
>> Nick
>>
>> On Sat, Apr 9, 2011 at 4:29 PM, emanuele mazzini
>> <[email protected]> wrote:
>>>
>>> i do have the following problem in generating a new variable: I need
>>> to generate a variable which takes the value of the average values of
>>> the five preceding years of another variable that I have in my
>>> dataset.
>>> In other words, supposing my new variable is y at time t, I need that
>>> its value is equal to the average of another variable (say x) from t-1
>>> to t-5.
>>>
>>> Since I am just beginning in using Stata anyone knows how can I deal
>>> with this or find any help?
>>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```