Dear Statalisters,
I have a panel dataset and I'm struggling to generate the averages of the last 3 observed values of a variable due to the missing values present in the data.
Here is a simplified example data:
clear
input int(id year x_1)
1 1992 .
1 1993 120
1 1994 179
1 1995 200
1 1996 .
1 1997 210
1 1998 180
1 1999 230
1 2000 .
2 1992 .
2 1993 .
2 1994 230
2 1995 308
2 1996 80
2 1997 149
2 1998 .
2 1999 .
2 2000 .
3 1992 .
3 1993 .
3 1994 190
3 1995 .
3 1996 .
3 1997 180
3 1998 210
3 1999 .
3 2000 .
end
I'd like to generate a new column x_2 which averages the last 3 observed values for x_1 for each id. So for id 1, x_2 will have a value of 206.67 ranging from 1992-2000.
Any help is appreciated. Thank you.
Related Posts with Generating the average of the last 3 observed values of a variable in a panel data
Tabulating country fixed effects predicted in StataHello to everyone again, For my thesis, I aim on deriving country fixed effects. A suggested by a f…
Increasing space for column and row labels when tabulating resultsHello everyone, I am doing five fixed effects regressions with five different dependent variables. …
spmap - adding a square to the mapDear Statalist How can I draw a square around a specific section on the map without using the graph …
Xttest2 in a non panel data regression?I tried to perform a xttest2, and this was the output: Error: too few common observations across pa…
Plotting results after running mixed effects modelHi All I've run the follwoing mixed effects model with linear splines to look at change in multimor…
Subscribe to:
Post Comments (Atom)
0 Response to Generating the average of the last 3 observed values of a variable in a panel data
Post a Comment