Hello,

I have a large dataset of 59,000,000 observations.

There are a 50 variables each beginning with the word qof. Some of the observations have missing values. I need to replace the missing values with the mean by group (healthgroup), For an individual missing variable I can do this easily enough I think:

egen missqof1 = mean(qof1), by(health_org)
replace qof1 = missqo1 if missing(qof1)

my problem is that I don't want to type this 50 times for each of the 50 variables, so I want to do this in a loop I think:


foreach var of varlist qof* {

replace qof* = mean(qof*) if missing(qof*)
}

I am not familiar with loops - and not sure how to make the code work.