Multiple Means of a List

Yida

New Member
#1
I have a list of years and coefficients and would like to create new variables, year2 and average coefficients. For each year2 entry, I want to have the average coefficients entries to be the average coefficients for the past 3 years. For example:
Code:
year coef1 coef2 coef3       year2 coef1_avg coef2_avg coef3_avg
01    .5      .7       .3       01      .       .        .
02    .8      .2       .4       02      .       .        . 
03    .3      .8       .3       03      .       .        . 
04    .9      .9       .7       04    .533    .566     .333               
05    .4      .4       .3       05    .666    .633     .466
06    .1      .2       .2       06    .533    .700     .433 
...
How should I implement this?
Thanks for the help.
 

bukharin

RoboStataRaptor
#2
Assuming you have one row per year, you could just do something like:
gen coef1_avg=(coef1[_n-1] + coef1[_n-2] + coef1[_n-3])/3