Announcement

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

  • panel data: mean without missing values

    Hi everybody,

    I have a dataset for about 200 countries ("id") for the years 1960 to 2013 for different variables. I want to calculate the mean over the years 1980-2005 for one variable (here "data1250") and for each country but only if for country i there is no missing data for the years 1980-2005. If at least one data (i.e. year 1999) is missing, I do not want stata to calculate the mean. I was able to calculate the mean for all data with the following:

    bysort id: egen mdata1250 = mean(data1250) if year>=1980 & year<=2005

    However, I wasn't able to figure out how to keep stata from calculating the mean if data was missing. Could anybody please help me?

    Thank you

  • #2
    I assume that your dataset does not have a missing value (".") present for data1250, but that the observation itself is not present. So,
    Code:
    bysort id: egen byte year_tally = count(year) if inrange(year, 1980, 2005)
    by id: egen double mdata1250 = mean(data1250) if inrange(year, 1980, 2005) & year_tally == 26

    Comment


    • #3
      works perfectly! Thank you very much!

      Comment

      Working...
      X