Hello everybody,
I am working on a panel dataset consisting of individual-by-month data for one year. I have attached a picture of its structure and I would really like your help.
I have a variable - Branche - which contains information on the individuals workplace/industry each month. It is a six digit code and it is changes in this variable, I would like to count by individual under three certain conditions.
First of, I would like to count the total number of changes in Branche (going from six digits to another six digits). A change from six digits to missing is not relevant, but from missing to six digits is - unless the individual has missing for the first months and then has a six digit code. In other words it is only going from sex digits to another with "missing breaks" being welcome. Check out the column "Count" in the picture.
Second of, another variable - Service - takes on either "B" or "OF". If Service is "OF" then Branche typically is missing (.). I would like to count each time Branche changes from six digits to another without a "missing break" due to "OF" in Service. Check out the column "B-to-B".
Third of, I would like to do the opposite of "second" and count each time it goes B-to-OF/missing-to-B, i.e. from a six digit code to missing and to another six digits again. Check "B-to-OF-to-B".
I have tried replacing with multiple conditions on Branche and Service, but is does not seem to do the trick correctly. One example of code is:
gen b2b = 0
by ID: replace b2b = 1 if Branche[_n] != Branche[_n-12] & Branche[_n] !=. & Branche[_n-12] != . & Service[_n-1/12] != "OF" & b2b[_n-1/11] != 1
The last part of the code is to secure that a situation with ten missing (from _n = 2 to _n=11) is still being counted if _n = 1 and _n-12 are different in terms of Branche. And to be secure that a change is only counted once.
I am working on a panel dataset consisting of individual-by-month data for one year. I have attached a picture of its structure and I would really like your help.
I have a variable - Branche - which contains information on the individuals workplace/industry each month. It is a six digit code and it is changes in this variable, I would like to count by individual under three certain conditions.
First of, I would like to count the total number of changes in Branche (going from six digits to another six digits). A change from six digits to missing is not relevant, but from missing to six digits is - unless the individual has missing for the first months and then has a six digit code. In other words it is only going from sex digits to another with "missing breaks" being welcome. Check out the column "Count" in the picture.
Second of, another variable - Service - takes on either "B" or "OF". If Service is "OF" then Branche typically is missing (.). I would like to count each time Branche changes from six digits to another without a "missing break" due to "OF" in Service. Check out the column "B-to-B".
Third of, I would like to do the opposite of "second" and count each time it goes B-to-OF/missing-to-B, i.e. from a six digit code to missing and to another six digits again. Check "B-to-OF-to-B".
I have tried replacing with multiple conditions on Branche and Service, but is does not seem to do the trick correctly. One example of code is:
gen b2b = 0
by ID: replace b2b = 1 if Branche[_n] != Branche[_n-12] & Branche[_n] !=. & Branche[_n-12] != . & Service[_n-1/12] != "OF" & b2b[_n-1/11] != 1
The last part of the code is to secure that a situation with ten missing (from _n = 2 to _n=11) is still being counted if _n = 1 and _n-12 are different in terms of Branche. And to be secure that a change is only counted once.
Comment