Hi there,
I would like to compute the within-firm covariance(wage_t, wage_{t-1}) for each year. I listed some sample panel data below.

year firm job wage
2000 1 1 10
2000 1 2 11
2000 1 3 10
2000 2 4 8
2000 2 5 4
2000 2 6 11
2000 2 7 5
2000 3 8 5
2000 3 9 9
2000 4 10 7
2000 4 11 8
2001 1 1 10.5
...
...

If it were not covariance, I could have just used egen ... by (year firm) to generate a wage moment for each firm each year and then take the mean across firms to get one number for each year to compute the average within-firm wage moment for each year. With tabstat I could easy print the within-firm moment by year as a table. But egen does not go with corr command.

But since I need covariance, so far I tried bysort firm year: cor wage lagwage, covariance, it gives me a matrix for each firm-year pair, but then I will have to manually select the covariance I need from a matrix in each firm-year pair and then compute the average cov across firms for each year. There are too many firms and year, and thus their pairs in the real data to make this an efficient approach. Any better solutions that allow me to calculate and print the results out by year as a table?

Thanks!