Announcement

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

  • Create mean variables of a subgroup for the whole group

    Dear all,

    I have data on education attainment per year and state and I want to compute the percentage of people who are 25 or above and have at least a Bachelors degree in a given state a given year. So far I have tried the following:

    Code:
    gen percedu = 0
    replace percedu = 1 if educ>=110
    egen meanpercedu = wtmean(percedu) if age>=25, weight(asecwt) by (statefip year)
    The issue is that for all observations in a given state and given year whose age is less than 25, I have a missing value. What I want is to have the same value instead of missing values for all. Is there a way to compute this?

    Thank you in advance.

    Best regards,
    Alexandros Achilleos

  • #2
    A detail here of importance is that you are using the community-contributed package _gwtmean from SSC but do not explain that: compare the advice at FAQ Advice #12.

    That should not bite. You can rewrite your code as

    Code:
    gen percedu = edu >= 110
    
    egen meanpercedu = wtmean (cond(age >= 25, percedu,.)), weight(asecwt) by(statefip year)
    See
    https://www.stata.com/support/faqs/d...rue-and-false/ and https://www.stata-journal.com/articl...article=dm0055 (especially Sections 9 and 10).

    (Your variable names imply percents, but you are calculating proportions. )

    Comment


    • #3
      Dear Mr Cox,

      You are right, I forgot to mention it. The code works perfectly. Thank you very much!

      Comment

      Working...
      X