Hi all,

I am relatively new to Stata and have a question regarding the generation of a new variable. Basically I have a list of observations that could be simply illustrated as follows:


Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float(year_event c2015 c2014 c2013 c2012 c2011)
2015 7 2 3 4 7
2014 6 8 8 8 8
2013 4 9 9 4 9
2014 5 7 4 7 5
2016 8 5 7 3 1
end
I would want to generate a variable cyear_1 and cyear_2, which is equal to the c-variable of the year 1 year before and 2 years before the year_event variable. So it would look like:


Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float(year_event c2015 c2014 c2013 c2012 c2011 cyear_1 cyear_2)
2015 7 2 3 4 7 2 3
2014 6 8 8 8 8 8 8
2013 4 9 9 4 9 4 9
2014 5 7 4 7 5 4 7
2016 8 5 7 3 1 8 5
end
Since the potential list of variables to choose from is much longer than this variable, as it goes from 2017 to 1997, I am trying to find a way to link my egen command to the years in the variable names but I have not yet found how to do this on this forum. Could anyone help me with this issue?

Kind regards