Hi all,
I am trying to create hedge portfolios for different asset pricing models like Daniel, Mota, Rottke and Santos (2020). First companies are put into portfolios on July each year, based on firm characteristics like book-to-market ratio. The first portfolio consists of firms with low-book-to-market ratio (1), the second with high book-to-market ratio (2). A next step in the procedure is to get a new variable which contains the return on the low book-to-market portfolio (1), for the 5 years preceding the formation of the portfolio.
Below a hypothetical panel dataset is shown because my own dataset is too large to show. In short I want to generate a new variable that gives me the return of portfolio 1 and 2 in the years preceding portfolio formation in the year 2013. This is a simplified version of the question because I want to do this for around 20 years and this will result in overlapping in the newly generated variables but I want to overcome this by doing this separately for every year.

ID Month year return Portfolio
1 jul 2010 0.02 2
1 jul 2011 0.02 2
1 jul 2012 0.02 1
1 jul 2013 0.02 1
2 jul 2010 0.03 2
2 jul 2011 0.03 2
2 jul 2012 0.03 1
2 jul 2013 0.03 1
3 jul 2010 0.05 1
3 jul 2011 0.05 1
3 jul 2012 0.05 2
3 jul 2013 0.05 2
4 jul 2010 0.4 1
4 jul 2011 0.4 1
4 jul 2012 0.4 2
4 jul 2013 0.4 2
I hope this question is clear enough and thanks in advance for reading this.

Kind regards,
Niels