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?
Related Posts with Calculate mean when values are missing
Create a variable counting occurrencesHello, I'm using Stata for the first time and encountered some difficulties. I'm working with Stat…
Stata Codes for Post-estimation TestsGood day everyone, I am currently working on the focus - Working Capital Management and Performance …
Difference-in-difference fixed effects for discontinued treatmentDear all, I am trying to run a difference-in-difference fixed-effects model to estimate the effect …
Matrix of matricesDears I would like to create a matrix containing the results of other matrices generated after the …
panel data analysisHello, I am performing analysis on an unbalanced panel dataset, of 160 firms which have data availa…
Subscribe to:
Post Comments (Atom)
0 Response to Calculate mean when values are missing
Post a Comment