Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • how to count observations for workers changing firms

    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:
    Code:
    egen var1 = group(id_person2 id_firm2)
    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.
    Code:
    bys id_person2 id_firm2: gen var2 = _n
    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.

    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

  • #2
    Code:
    help tsspell
    this is a user-written program so you may need to install; use -search- or -findit- to locate and install

    Comment


    • #3
      Rich gives excellent advice. The same principles are shown here:

      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
      
      bysort id_person2 (occ) : gen wanted = sum(id_firm2 != id_firm2[_n-1])
      
      list id_person2 id_firm2 wanted, sepby(id*)
      
      
           +------------------------------+
           | id_per~2   id_firm2   wanted |
           |------------------------------|
        1. |       12       6364        1 |
           |------------------------------|
        2. |       12       5334        2 |
        3. |       12       5334        2 |
           |------------------------------|
        4. |       12       5333        3 |
        5. |       12       5333        3 |
           |------------------------------|
        6. |       49        975        1 |
           |------------------------------|
        7. |       49       4717        2 |
           |------------------------------|
        8. |       49       2532        3 |
           |------------------------------|
        9. |       49         23        4 |
           |------------------------------|
       10. |       49        507        5 |
           |------------------------------|
       11. |       49       6728        6 |
           |------------------------------|
       12. |       49        839        7 |
           |------------------------------|
       13. |       49       6348        8 |
           |------------------------------|
       14. |       49       2342        9 |
           |------------------------------|
       15. |       52       1127        1 |
       16. |       52       1127        1 |
           |------------------------------|
       17. |       52       3780        2 |
           |------------------------------|
       18. |       52       3804        3 |
       19. |       52       3804        3 |
       20. |       52       3804        3 |
       21. |       52       3804        3 |
           |------------------------------|
       22. |       52       6538        4 |
       23. |       52       6538        4 |
       24. |       52       6538        4 |
           |------------------------------|
       25. |       52       5698        5 |
           |------------------------------|
       26. |       52       3777        6 |
       27. |       52       3777        6 |
           |------------------------------|
       28. |       52       6623        7 |
       29. |       52       6623        7 |
           |------------------------------|
       30. |       52       5998        8 |
           |------------------------------|
       31. |       52       6622        9 |
           |------------------------------|
       32. |       52       6623       10 |
       33. |       52       6623       10 |
           +------------------------------+
      and discussed at https://www.stata-journal.com/articl...article=dm0029

      Comment

      Working...
      X