Hello,
I am fairly new to STATA and have spent awhile trying to solve this, but can't seem to figure it out. I am working on a questionnaire that has multiple standardized measures on it. Some participants did not fill out all the questions, leaving missing values. However, if they are sufficiently completed I can calculate the mean and replace the missing values with the mean.
In this example, the variables are ces1_1 through ces20_1, the mean is ces_mean_1, and the count of valid values is ces_count_1. I have tried two methods, but I don't know if either is even the appropriate approach:
1) Using the recode command, but I believe that it won't let me replace the missing value with the value of another variable, but instead only a specific number:
recode ces1_1-ces20_1 (missing = ces_mean_1) if ces_count_1 >= 16
This gives an error of unknown el ces_mean_1 in rule
2) Using the foreach command:
foreach var of varlist ces1_1-ces20_1 {
replace `.' = ces_mean_1 if ces_count_1 >=16
}
However, it gives me an error of "varlist required." I used trace to find out where the error was occurring and this is what it gave me:
. foreach var of varlist ces1_1-ces20_1 {
2. replace `.' = ces_mean_1 if ces_count_1 >=16
3. }
- foreach var of varlist ces1_1-ces20_1 {
- replace `.' = ces_mean_1 if ces_count_1 >=16
= replace = ces_mean_1 if ces_count_1 >=16
varlist required
}
However, I don't see where a varlist would be needed in that line.
Thank you for any help,
Jordan
I am fairly new to STATA and have spent awhile trying to solve this, but can't seem to figure it out. I am working on a questionnaire that has multiple standardized measures on it. Some participants did not fill out all the questions, leaving missing values. However, if they are sufficiently completed I can calculate the mean and replace the missing values with the mean.
In this example, the variables are ces1_1 through ces20_1, the mean is ces_mean_1, and the count of valid values is ces_count_1. I have tried two methods, but I don't know if either is even the appropriate approach:
1) Using the recode command, but I believe that it won't let me replace the missing value with the value of another variable, but instead only a specific number:
recode ces1_1-ces20_1 (missing = ces_mean_1) if ces_count_1 >= 16
This gives an error of unknown el ces_mean_1 in rule
2) Using the foreach command:
foreach var of varlist ces1_1-ces20_1 {
replace `.' = ces_mean_1 if ces_count_1 >=16
}
However, it gives me an error of "varlist required." I used trace to find out where the error was occurring and this is what it gave me:
. foreach var of varlist ces1_1-ces20_1 {
2. replace `.' = ces_mean_1 if ces_count_1 >=16
3. }
- foreach var of varlist ces1_1-ces20_1 {
- replace `.' = ces_mean_1 if ces_count_1 >=16
= replace = ces_mean_1 if ces_count_1 >=16
varlist required
}
However, I don't see where a varlist would be needed in that line.
Thank you for any help,
Jordan
Comment