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
Comparing two probit models with clustered standard errorsHey everyone, I would like to compare two probit models (that are nested) to see whether the additi…
Testing of adjusted Kaplan-Meier survivalHow to test for adjusted kaplan-meier survival analysis ? I am able to create adjusted KM graph but …
Marginsplots for different values of xHello, first post so apologies if something similar has been asked before. I'm investigating the rel…
'End Duplicates' error in mata programing Hello, everyone. I'm Zhang. I would like to ask you a question about an 'End Duplicates' error in …
Generating data according to a patternHello list, I am trying to generate data according to the following pattern: Obs A B C ------------…
Subscribe to:
Post Comments (Atom)
0 Response to Calculate mean when values are missing
Post a Comment