Announcement

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

  • Replace missing counts with existing counts within a column

    Dear Statalists,

    I got a list of companies and directors, whereby the directors are of different type. For each director type, I would like to calculate the number of directors per company per year.
    I´ve done it but the problem is now with missing values. In the dataset below total_type_E counts the number of directors with type E, but only for the lines where the director is of type E. I want this number in every line for that company for that year. So from line 1 to 8, the column total_typeE should be 3.

    Do you have any ideas?


    Code:
    clear
    input float year long permno double didown float(count_dir total_dir) str5 dir_type float(count_typE total_typE indep)
    1996 10016  4336600 1 8 "E" 1 3 0
    1996 10016     3241 2 8 "E" 2 3 0
    1996 10016  4336700 3 8 "E" 3 3 0
    1996 10016  4001200 4 8 "I" . . 1
    1996 10016 20037900 5 8 "I" . . 1
    1996 10016  4336900 6 8 "I" . . 1
    1996 10016  3489000 7 8 "L" . . 0
    1996 10016  4337000 8 8 "L" . . 0
    end

  • #2

    Code:
    foreach x in E I L {
        egen wanted_`x' = total(dir_type == "`x'"), by(permno year)
    }
    A fairly systematic survey of common tricks in this territory was given in https://www.stata-journal.com/articl...article=dm0055 (pdf universally visible)

    Comment


    • #3
      Thank you Nick!

      Meanwhile I found another solution:


      Code:
      replace count_typE = 0 if count_typE ==.
      by year permno: egen total_typE = max(count_typE)
      Sorry to bother you guys before finding out by myself!

      Comment


      • #4
        In #3 the replace line is not needed as the max() will ignore missings unless all values are missing. Note that #2 gives the solution in one line any way.

        Comment

        Working...
        X