Announcement

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

  • asking about the code to identify ( the average of firm in each industry )

    Hi, I'm actually a bit confused about the difference between calculating the average of firms in each industry for each year where the CSO presence variable equals 1 and calculating the percentage. In the code below, I calculated the percentage of firms in each industry per year with a CSO presence equal to 1. Can you help clarify how to compute the average instead?

    This is the way i calculated the The percentage of firms in each industry in each year where the cso presence variable is equal to 1

    * drop if industry identifier is missing
    drop if SIC_WRDS_2 == .
    keep if year >= 2004 & year <= 2022

    * Count the number of firms in each industry in each year where the cso presence variable is equal to 1
    capture bys SIC_WRDS_2 year : egen Instrumental_based_on_CSO_Count = count(csopresence1 / (csopresence1==1))

    * Count the total number of firms in each industry and each year where cso is not missing
    bys SIC_WRDS_2 year : egen total_firms_industry_year = count(csopresence1)

    * The percentage of firms in each industry in each year where the cso presence variable is equal to 1
    gen CSO_Percentage = Instrumental_based_on_CSO_Count / total_firms_industry_year

    can you help clarify how to compute the average of firms in each industry in each year where the cso presence variable is equal to 1

  • #2
    Average of what when csopresence == 1? Assuming you want the average of some variable x, it would be
    Code:
    by SIC_WRDS_2: egen wanted = mean(cond(csopresence == 1, x, .))
    By the way, it looks to me like your code calculating a percentage does not do so: it counts the proportion (ranging from 0 to 1). To get the percentage, if that's what you need, multiply the result by 100.

    Comment


    • #3
      Thank you, Clyde, for your suggestion. What I'm looking for is the average number of firms in each industry for each year where the CSO presence variable equals 1.

      i write this code can you check it please

      by SIC_WRDS_2 year: egen avg_firmsss_with_cso = mean(csopresence1 == 1 )

      Comment


      • #4
        The code in #3 yields a proportion, I wouldn't call that an average number of firms, but I don't know whether it's what you seek.

        Nothing beats a toy example. For once fake data with the right structure and a calculation showing what you want may be as or more informative than real data.

        Comment


        • #5
          As the other answers have said, egen is your friend here. But I kind of have a different issue. When I hear the average number of firms in each industry, I hear that as adding up all the number of firms in an industry divided by the observations. But this doesn't really make sense. Let's say the town I live in has 5 grocery stores and 2 auto shops (it certainly has WAY more than that). What's the average in this case? You really can't have one that isn't a proportion, in some sense.

          We can have average income by industry, or average number of employees across industries. I'll give another example. Say for some reason we're surveying hospital patients. We can't ask them "what's the average number of doctor visits you've had" since that's sort of just a raw number. We can ask the average number of monthly visits, but the average number of all visits just feels weird

          Comment

          Working...
          X