Dear all, I have a longitudinal data of workers and occasions. I would like to count (or generate an id) for the number of firms within individuals, but only when the firm change. I mean, for individual 12 (see dataex below), the first occasion (occ) is firm "6364" and thus it should count as number 1. For his/her second and third obs. I need the variable to be 2 (in the two obs). And for the last two obs. of the same individual to be 3. However, there are individuals like the 52, which work for the same firm in some occasion, then they change firms, and then come back to some previous firms (see obs. 14-15 and 18-19).
I have tried something like:
but the id it creates is repeated in those obs. from person 52 (basically does not respect the occasion variable), and it is not reseated for each person.
Then I tried this another way, but even though this reseat the counting by each person, it does not assign a consecutive order to the change of firms within person.
Any idea of what to do for counting when a person change from one firm to another (different) firm within person?
Thanks for the help.
I have tried something like:
Code:
egen var1 = group(id_person2 id_firm2)
Then I tried this another way, but even though this reseat the counting by each person, it does not assign a consecutive order to the change of firms within person.
Code:
bys id_person2 id_firm2: gen var2 = _n
Thanks for the help.
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str20 id_person2 float occ str20 id_firm2 float(var1 var2) "12" 1 "6364" 1321 1 "12" 2 "5334" 1320 2 "12" 3 "5334" 1320 1 "12" 4 "5333" 1319 2 "12" 5 "5333" 1319 1 "49" 1 "975" 10421 1 "49" 2 "4717" 10416 1 "49" 3 "2532" 10415 1 "49" 4 "23" 10413 1 "49" 5 "507" 10417 1 "49" 6 "6728" 10419 1 "49" 7 "839" 10420 1 "49" 8 "6348" 10418 1 "49" 9 "2342" 10414 1 "52" 1 "1127" 10623 2 "52" 2 "1127" 10623 1 "52" 3 "3780" 10625 1 "52" 4 "3804" 10626 4 "52" 5 "3804" 10626 3 "52" 6 "3804" 10626 2 "52" 7 "3804" 10626 1 "52" 8 "6538" 10629 3 "52" 9 "6538" 10629 1 "52" 10 "6538" 10629 2 "52" 11 "5698" 10627 1 "52" 12 "3777" 10624 2 "52" 13 "3777" 10624 1 "52" 14 "6623" 10631 2 "52" 15 "6623" 10631 4 "52" 16 "5998" 10628 1 "52" 17 "6622" 10630 1 "52" 18 "6623" 10631 3 "52" 19 "6623" 10631 1 end
Comment