Hi

I have a longitudinal data set where I am following people going through different states over time, kind of like this:

Code:
clear

input id year state
1 2001 1
1 2002 2
1 2003 2
2 2001 1
2 2002 1
2 2003 1
3 2001 3
3 2002 2
3 2003 2
4 2001 3
4 2002 2
4 2003 3
end

label define statelabel 1 "Working" ///
                        2 "Retired" ///
                        3 "Homemaker" ///
                        4 "Retired after working" ///
                        5 "Retired after homemaking"

label val state statelabel
I.e., it looks like this:

Code:
. list, sepby(id)

        
id   year    state    
        
1.   1   2001    Working    
2.   1   2002    Retired    
3.   1   2003    Retired    
        
4.   2   2001    Working    
5.   2   2002    Working    
6.   2   2003    Working    
        
7.   3   2001    Homemaker    
8.   3   2002    Retired    
9.   3   2003    Retired    
        
10.   4   2001    Homemaker    
11.   4   2002    Retired    
12.   4   2003    Homemaker
Note how the codes 4 and 5 of the "state" variable aren't assigned yet.

For doing so, I could so far only think of the following approach:

Code:
bys id (year): replace state = 4 if state[_n-1] == 1 & state == 2
bys id (year): replace state = 5 if state[_n-1] == 3 & state == 2
This of course doesn't capture those who are unemployed for more than a year (i.e. observations 3 and 9):

Code:
. list, sepby(id)
       
id   year    state    
        
1.   1   2001    Working    
2.   1   2002    Retired after working    
3.   1   2003    Retired    
        
4.   2   2001    Working    
5.   2   2002    Working    
6.   2   2003    Working    
        
7.   3   2001    Homemaker    
8.   3   2002    Retired after homemaking    
9.   3   2003    Retired    
        
10.   4   2001    Homemaker    
11.   4   2002    Retired after homemaking    
12.   4   2003    Homemaker
Is there a cleverer solution that solves my problem?

Thanks for your consideration
Go