Dear all,

I am currently working with a survey dataset that follows people over time and looks as follows:
person-id year state inmigration outmigration
1 1991 1 0 0
1 1992 1 0 0
1 1993 1 0 0
2 1991 1 0 0
2 1992 1 0 0
2 1993 2 1 1
3 1991 1 0 0
3 1992 1 0 0
3 1993 2 1 1
My task is to build inmigration and outmigration rates for every state and year. Therefore, I thought that I could generate a variable "inmigration" that equals 1 if state_t ≠ state_t-1. As a next step I could sum the observations by state and year to get amount of people migrating to a state. In the example above this would equal 2 for state 2 and year 1993 and 0 for all other years and states.
However, I also need to compute outmigration rates by state and year. This variable should also equal 2 for the year 1993, but should be reported for state 1 because these people are migrating frome state 1 to state 2 and are therefore leaving state 1. Does anyone know how to adress this problem?

I hope my question is clear.

Thanks for your help and best regards,

Leandro