Announcement

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

  • Calculate mean when values are missing

    I need to create a variable that is the mean of the observations from three other variables. I did this:
    gen CPI_foodweight_20142016 = (CPI_foodweight_2014+CPI_foodweight_2015+CPI_foodw eight_2016)/3

    However, some of the the values are missing for one or two of these three variables, which leads to a missing value on the varibale I'm creating. I would like to find a way to make Stata calculate the mean with what is available, meaning:
    -If there are no missing values, calulate the mean using all three observations
    -If one observation is missing, calculate the mean of the two observations that are available
    -If two observations are missing, make the observation on the new variabe equal to the observation from the variable that isn't missing

    Is there any way I could code for that?

  • #2
    -egen, rowmean- does what you want.

    Comment


    • #3
      Aline:
      as an aside to Joro's helpful advice, please note that Stata applies listwise deletion by default; hence, observations with missing value will be ruled out from subsequent calculation, as you can see from the following toy-example:
      Code:
      . use "C:\Program Files\Stata16\ado\base\a\auto.dta"
      (1978 Automobile Data)
      
      . sum rep78
      
          Variable |        Obs        Mean    Std. Dev.       Min        Max
      -------------+---------------------------------------------------------
             rep78 |         69    3.405797    .9899323          1          5
      
      . sum price
      
          Variable |        Obs        Mean    Std. Dev.       Min        Max
      -------------+---------------------------------------------------------
             price |         74    6165.257    2949.496       3291      15906
      
      .
      Kind regards,
      Carlo
      (Stata 19.0)

      Comment


      • #4
        Wonderful, thank you both!

        Comment

        Working...
        X