Dear all,

I have some panel data containing countries and years. I need to build three-year time windows. At the moment, I am building the three-year windows starting from the first year. However, this might be a problem if the number of years available for my variable is not a multiple of 3. In that case, the most recent time windows might contain in fact only 1 or 2 years. I would like to revert this, i.e. start building the time windows from the most recent year, so that the last time windows always contains three years. I do not care if the oldest time window has less than 3 years.

Please find below an example of my data and how I am constructing the time windows at this moment. The example shows that the last time window (window n. 4) only contains the year 2019.

Code:
clear
input str2 country str4 year
"AA" "2010"
"BB" "2010"
"CC" "2010"
"AA" "2011"
"BB" "2011"
"CC" "2011"
"AA" "2012"
"BB" "2012"
"CC" "2012"
"AA" "2013"
"BB" "2013"
"CC" "2013"
"AA" "2014"
"BB" "2014"
"CC" "2014"
"AA" "2015"
"BB" "2015"
"CC" "2015"
"AA" "2016"
"BB" "2016"
"CC" "2016"
"AA" "2017"
"BB" "2017"
"CC" "2017"
"AA" "2018"
"BB" "2018"
"CC" "2018"
"AA" "2019"
"BB" "2019"
"CC" "2019"
end

egen period = group(year)
gen time_window = 1 + floor((period-1)/3)