Announcement

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

  • create mean variable based on subset of other variable

    Hello,

    I have data which contains values by financial year. I wanted to create a variable that contains the mean value based on another variable. My code is as follows:


    bysort prov_trunc : egen mean_Q = mean( quantity)

    This worked fine with my data. However, I realised I only want the mean to be based on 2 of the 4 financial years in my data: I amended my code as follows:

    bysort prov_trunc : egen mean_Q2 = mean( quantity)if Financial_Year =="2018/19" | Financial_Year =="2019/20"

    This did create a mean variable for based on the two years in question, but only for those observations in those two years. What I would like is variable containing the average, but the average across just those two years.

    Can anyone please advise on how to best amend my code?

  • #2
    Code:
    bysort prov_trunc: egen mean_Q = mean(cond(inlist(Financial_Year, "2018/19", "2019/20"), quantity, .))
    For an elaboration, see https://www.stata-journal.com/articl...article=dm0055

    Comment

    Working...
    X