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
weighted varibles for latent class modelHi Scholars, I have a dataset that overrepresented respondents in some of important socio-demograph…
Distance between two coordinatesGoodmorning, I want to calculate the distance between two x,y coordinates. However, I have a list of…
Interaction Using Single Hashtag (#)Dear Experts, My objective is to get two coefficients with corresponding p-values before and after …
check if variable is present and append to local varlistHi, I want to loop over all values of a local varlist and check if a variable of that name is prese…
Coefplot - without any FEDear Statalist users, I'm trying to make a plot of my regression results using coefplot. I have dat…
Subscribe to:
Post Comments (Atom)
0 Response to Calculate mean when values are missing
Post a Comment