Announcement

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

  • Only count certain rows

    Hi all,

    I have the following data structure:
    DirectorID Mgt_dummy Year
    1 1 2000
    1 1 2001
    1 0 100000
    2 1 2003
    2 1 2004
    2 0 100000
    Where Mgt_dummy is a dummy with a value of 0 if the Director was a manager in a certain year.

    I want Stata to create a new variable counting the number of years a director has been manager. For instance, Director 1 is a manager for 2 years, and director 2 as well.


    My current code is:

    sort DirectorID year
    by DirectorID: gen ManagerYears= _N
    by DirectorID: keep if _n == 1


    But with this code, my variable ManagerYears counts all rows, not just those where the Mgt_dummy variable has a value of 1.

    How can I do this?


    Thank you so much!

    Best,

    Carla

  • #2
    If mgt_dummy can only be 1 or 0, and there is only one row per manager-year pair, you should be able to count the amount of years a director was manager by summing mgt_dummy:

    Code:
    bysort DirectorID: egen ManagerYears = total(mgt_dummy)

    Comment


    • #3
      That's exactly it, thank you!

      Comment

      Working...
      X