Announcement

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

  • Changing a nominal variable (like firm ID) into a dummy variable?

    Hi,
    I need some help to create a dummy variable from a nominal variable. I want to make a dummy for my person IDs as mover or stayer. If the person stays in the same firm, then the person is a stayer (value 0) and if s/he chnages the firm, s/he is a mover (with value 1). I would appreciate to know the command or a relevant reference to read aboout it.
    Person ID Firm ID Year stayer/mover
    1 100 2015 0
    1 100 2016 0
    1 115 2017 1
    1 116 2018 1
    2 113 2015 0
    2 118 2016 1
    2 118 2017 0
    2 140 2018 1
    Last edited by Alp Marta; 17 Nov 2022, 08:11.

  • #2
    If I have understood your question correctly, you only care about whether the person changes firms in their entire history, then this will suffice. The logic is straightforward: for each observation, return a zero if the person stays at the same firm as their first recorded year, or else they moved and a 1 is returned. The max value over all years for the person then records if they ever moved, or not.

    Code:
    input byte(pid) int(firmid year)
    1 100 2015
    1 100 2016
    1 115 2017
    1 116 2018
    2 113 2015
    2 118 2016
    2 118 2017
    2 140 2018
    end
    
    bys pid (year) : egen byte mover = max(cond(firmid != firmid[1], 1, 0))
    list
    Result

    Code:
    . list
    
         +-----------------------------+
         | pid   firmid   year   mover |
         |-----------------------------|
      1. |   1      100   2015       1 |
      2. |   1      100   2016       1 |
      3. |   1      115   2017       1 |
      4. |   1      116   2018       1 |
      5. |   2      113   2015       1 |
         |-----------------------------|
      6. |   2      118   2016       1 |
      7. |   2      118   2017       1 |
      8. |   2      140   2018       1 |
         +-----------------------------+

    Comment


    • #3
      Thank you Leonardo for the quick feedback. I just want to compare the firm change with previous year not necessay the firs recorded year. For example, person 2 in 2018 changed his firm from 118 to 140, so he is in this year a mover and gets the value 1.

      Comment


      • #4
        In that case you need the following.

        Code:
        bys pid (year) : gen byte mover = cond(_n>1 & firmid != firmid[_n-1], 1, 0)

        Comment


        • #5
          perfect and many thanks

          Comment

          Working...
          X