Announcement

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

  • how to generate a count variable

    Hello everyone,

    I have the following data set:
    Company name Name CEO CFO Year Quater Gender
    Company A John A Yes no 2019 1 Male
    Company A Bill B no Yes 2019 1 Male
    Company A Hill C NO YES 2019 1 Female
    Company A Jack D NO NO 2019 1 Male
    I would like to generate a gender power variable by counting the male managers of the company, for instant, for the above sample, the gender power should be 3 because there are 3 male managers in the company during that quarter.

    I have tried the following code:

    sort gvkey datacqtr

    . by gvkey datacqtr: count Male=="1"

    But it will not generate the variable I want.

    Thank you in advance.

  • #2
    Code:
    bys company year quarter: egen wanted = total(gender=="male")
    Assumes that gender is a string variable.

    Comment


    • #3
      Hi Lucus,

      What does your output look like? Do you get an error?

      Based on what you've posted, it doesn't look like you actually call the generate command anywhere. You sort the data by gvkey and datacqtr, and then get the count of men by these two variables, but you don't take the output from count and assign it anywhere, correct? Unfortunately, the Stata documentation on the count command is a little sparse, but it looks like an appropriate method is described in section 4.2 of this document: https://www.stata-journal.com/sjpdf....iclenum=pr0029

      Basically, you need to loop through your observations, generate the appropriate count for the given observation, extract the return value of count and assign it to the variable at that observation. Something like this might work:
      [CODE]
      . gen CountMen = 0
      forval i = 1/`=_N' {
      count if Gender == "Male" & CompanyName == CompanyName[`i']
      replace CountMen = r(N) in `i'
      }
      [\CODE]

      Edit: That is, if you wanted to use count for some reason. Otherwise Andrew's got it.
      Last edited by Daniel Schaefer; 28 May 2022, 15:51.

      Comment


      • #4
        Originally posted by Andrew Musau View Post
        Code:
        bys company year quarter: egen wanted = total(gender=="male")
        Assumes that gender is a string variable.
        Thank you Andrew, this is great! I got what I want!

        Comment


        • #5
          Originally posted by Daniel Schaefer View Post
          Hi Lucus,

          What does your output look like? Do you get an error?

          Based on what you've posted, it doesn't look like you actually call the generate command anywhere. You sort the data by gvkey and datacqtr, and then get the count of men by these two variables, but you don't take the output from count and assign it anywhere, correct? Unfortunately, the Stata documentation on the count command is a little sparse, but it looks like an appropriate method is described in section 4.2 of this document: https://www.stata-journal.com/sjpdf....iclenum=pr0029

          Basically, you need to loop through your observations, generate the appropriate count for the given observation, extract the return value of count and assign it to the variable at that observation. Something like this might work:
          [CODE]
          . gen CountMen = 0
          forval i = 1/`=_N' {
          count if Gender == "Male" & CompanyName == CompanyName[`i']
          replace CountMen = r(N) in `i'
          }
          [\CODE]

          Edit: That is, if you wanted to use count for some reason. Otherwise Andrew's got it.
          Thank you Daniel, Andrew's code help me to get what I want.

          Comment

          Working...
          X