Announcement

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

  • counting total of variable based on other variable

    Hello,

    i want to count the total M&A's (labeled as 1 in maactive) of a firm (based on gvkey) en generate a new variable. I tried the code

    egen totalma = count (maactive ==1), by (gvkey)

    but if I use this code, it counts all of the observations on maactive based on gvkey but I only need the ''1'' of the variable maactive.

    Does someone know what I am doing wrong?




    Click image for larger version

Name:	Schermafbeelding 2021-05-18 om 11.39.20.png
Views:	2
Size:	776.0 KB
ID:	1610331

  • #2
    Use -sum- instead:

    Code:
    clear
    input gvkey maactive
    1078 1
    1078 0
    1078 0
    1084 1
    1084 1
    1084 0
    end
    
    * egen totalma = sum(maactive), by(gvkey)
    * ^ Revised due to sum being non-documented. Use total()
    
    egen totalma = total(maactive), by(gvkey)
    Results:
    Code:
         +----------------------------+
         | gvkey   maactive   totalma |
         |----------------------------|
      1. |  1078          1         1 |
      2. |  1078          0         1 |
      3. |  1078          0         1 |
      4. |  1084          1         2 |
      5. |  1084          1         2 |
      6. |  1084          0         2 |
         +----------------------------+
    Last edited by Ken Chui; 18 May 2021, 06:46.

    Comment


    • #3
      Thanks! Works fine!

      Comment


      • #4
        You need total().

        count() counts non-missing values. As the result of a true-or-false comparison is 1 or 0, that is non-missing either way, with the result you report.


        sum() is an non-documented equivalent of total(), as from Stata 9. Given the choice, use documented features rather than what is not documented.
        Last edited by Nick Cox; 18 May 2021, 06:39.

        Comment


        • #5
          Originally posted by Nick Cox View Post
          You need total().

          count() counts non-missing values. As the result of a true-or-false comparison is 1 or 0, that is non-missing either way, with the result you report.


          sum() is an non-documented equivalent of total(), as from Stata 9. Given the choice, use documented features rather than what is not documented.
          Thanks Nick, I revised my code answer.

          Comment

          Working...
          X