Hi,

I have a panel dataset (example attached) which is tsset id year. employed is a binary variable recording if a person is employed (==1) or not (==0). tenure is a string variable that takes three values: either "full-time" or "part-time" if employed == 1 and "NA" if employed == 0.

If tenure == "NA", I want to implement a rule to replace tenure with either "full-time" or "part-time" as follows.
  • If for an person, employed == 0 for all values of year(e.g. for id == 10), do not replace tenure.
  • If for an person, employed = 0 for some values of year and 1 for others then
    • For values of tenure that are "NA" (i.e. person unemployed), replace tenure with the value of tenure corresponding the the last year the person was employed. For example, id == 11 is unemployed in years 6 and 7. Therefore, for these years I want to replace tenure == "full-time" because this person was last employed in year 5 and her tenure then was "full-time".
    • If for a employed == 0 observation, the person was never employed in the past (e.g. id == 11, years 1 and 2), then replace tenure with the value of tenure corresponding the the next time the person becomes employed. So for, id == 11 in years 1 and 2, I want to replace tenure == "part-time" because (1) this person was never employed in the past (relative to years 1, 2) and (2) this person was next employed in year 3 and her tenure then was "part-time".
Can you please help me with how I should go about coding this up?

Thanks,
Sharan


Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float(id time) str18 tenure float employed
 9 1 "full-time" 1
 9 2 "full-time" 1
 9 3 "full-time" 1
 9 4 "full-time" 1
 9 5 "full-time" 1
 9 6 "full-time" 1
 9 7 "full-time" 1
 9 8 "full-time" 1
 9 9 "NA"        0
10 1 "NA"        0
10 2 "NA"        0
10 3 "NA"        0
10 4 "NA"        0
10 5 "NA"        0
10 6 "NA"        0
10 7 "NA"        0
10 8 "NA"        0
10 9 "NA"        0
11 1 "NA"        0
11 2 "NA"        0
11 3 "part-time" 1
11 4 "full-time" 1
11 5 "full-time" 1
11 6 "NA"        0
11 7 "NA"        0
11 8 "part-time" 1
11 9 "NA"        0
22 2 "full-time" 1
22 3 "full-time" 1
22 4 "full-time" 1
22 5 "full-time" 1
22 6 "full-time" 1
22 7 "full-time" 1
22 8 "full-time" 1
22 9 "NA"        0
end