I have an unbalanced panel, say firms over years, each firm has a variable of interest. I would like to do the following: for each year t, consider t+1, compare these two years and get the common firms. Calculate the mean of variable of interest cross sectionally for these common firms at year t. Next, for year t+1, compare it with t+2, get another common set of firms, again, calculate the mean of the new common set of firms at year t+1. For example, as follows.
For year 2008, compare it with 2009. I have common firms: C, D. then I calculate C,D's mean (z+m)/2 for year 2008.
For year 2009, compare it with 2010. I have common firms: C, F. then I calculate C, F's mean (n+w)/2 for year 2009.
For year 2010, compare it with 2011. I have common firms: F, G. then i calculate F, G's mean (r+v)/2 for year 2010.

I understand this is a rather simple algorithm, but i didnt find a good way to do it in stata. Hope you could help. Many thanks.
year firm id variable of interest
2008 A x
2008 B y
2008 C z
2008 D m
2009 C n
2009 D q
2009 F w
2010 C t
2010 F r
2010 G v
2011 F u
2011 G p
2011 H j