Dear STATALIST Community,

I have an unbalanced panel, where individual status changes within groups. I want to compute mean score within plus/minus two periods whenever the status changes.

I tried to follow Cox (2002) and Cox (2011) and am able to determine switch using obs id [_n-1] but can't think of a way how to compute means around switches. I am attaching data file for your consideration.

For example, id 1’s status changes in year 2003, I would like to compute mean score within two pre-switch years, 2001 and 2002, and within two post-switch years, 2003 and 2004. The status changes again in year 2006, I now need to compute mean score within two pre-switch years -2004 and 2005 – and within two post-switch years - 2006 and 2007. Further, Id 2’s status changes in 2002, which has only one pre-switch year but two post-switch years. Likewise, the status changes again in 2007, with two pre-switch years and only one post-switch year.

Any help/guidance would be helpful!

Best wishes
Mg