Hi my data set looks like this
I want to generate liva_2, liva_3 and liva_5 and as the the name goes, for liva_2 for each gvkey i want to add the current year and prior year liva.
for liva_3 I will need to add current year and last 2 years liva ( for each gvkey)
for liva_5 I will have to add current year nd last 4 years liva.
please help me with the code.
| gvkey | fyear | liva | n | liva_2 | liva_3 | liva_5 |
| 1004 | 1999 | -0.81841 | 1 | |||
| 1004 | 2001 | -0.12328 | 2 | -0.94168 | ||
| 1004 | 2004 | -0.29572 | 3 | -0.41899 | -1.2374 | |
| 1004 | 2005 | 0.547 | 4 | |||
| 1004 | 2006 | -0.0125 | 5 | 15 | ||
| 1004 | 2008 | -0.26287 | 6 | |||
| 1004 | 2010 | 0.059042 | 7 | |||
| 1004 | 2011 | -0.35232 | 8 | |||
| 1004 | 2013 | 0.271471 | 9 | |||
| 1004 | 2014 | -0.06726 | 10 | |||
| 1004 | 2015 | -0.06743 | 11 | |||
| 1004 | 2016 | 0.211439 | 12 | |||
| 1004 | 2017 | -0.03207 | 13 | |||
| 1004 | 2018 | 0.107273 | 14 | |||
| 1013 | 1999 | 8.703646 | 1 | |||
| 1013 | 2001 | -26.258 | 2 | |||
| 1013 | 2002 | -5.24997 | 3 | |||
| 1013 | 2003 | 0.170867 | 4 | |||
| 1013 | 2004 | -1.53502 | 5 | |||
| 1013 | 2005 | 0.201077 | 6 | |||
| 1013 | 2006 | -2.42768 | 7 | |||
| 1013 | 2008 | -0.94143 | 8 | |||
| 1013 | 2009 | -0.24918 | 9 | |||
| 1034 | 1999 | -0.64264 | 1 | |||
| 1034 | 2000 | 0.686527 | 2 | |||
| 1034 | 2001 | -1.01673 | 3 | |||
| 1034 | 2002 | -1.24141 | 4 | |||
| 1034 | 2003 | 0.404838 | 5 | |||
| 1034 | 2005 | 0.769777 | 6 | |||
| 1034 | 2006 | -0.72259 | 7 | |||
| 1034 | 2007 | -0.49277 | 8 |
for liva_3 I will need to add current year and last 2 years liva ( for each gvkey)
for liva_5 I will have to add current year nd last 4 years liva.
please help me with the code.

Comment