Hello, I am working with a dataset that has some missing values that I'm trying to fill in by copying existing values. The data look like this:
I am using the following command to fill in the missing values of one column:
bysort ID: replace region= region[1] if region==.
This is working well. However, my data has many more geographic variables that I'd like to fill in in the same way without writing an individual line of code for each. I've attempted to write a for loop to do this:
foreach var in region state city {
bysort ID: replace var = var[1] if var==.
}
I'm getting the error "variable var not found." I've been following the syntax in this documentation and also referred to this example, though my variables all have different names and can't be easily renamed, so it's important that I'm able to specify a list of distinct variable names in the loop.
I've tried renaming the variables to be standard format (ex. geovar1, geovar2...etc) and am getting the error "variable geo_vars not found" from the following code:
local geo_vars region state city
local n = 1
foreach var of varlist geo_vars {
ren var geovar`n'
local `n' = n+1
}
foreach var of varlist geo1-geo10 {
bysort household_id: replace var = var[1] if var==.
}
This leads me to believe that I'm having a syntax error in my loop construction. I welcome any feedback or suggestions you may have. Thank you.
| ID | region | state | city |
| 1 | Northeast | New York | New York |
| 1 | . | . | . |
| 1 | . | . | . |
| 2 | Northeast | Massachusetts | Boston |
| 2 | . | . | . |
bysort ID: replace region= region[1] if region==.
This is working well. However, my data has many more geographic variables that I'd like to fill in in the same way without writing an individual line of code for each. I've attempted to write a for loop to do this:
foreach var in region state city {
bysort ID: replace var = var[1] if var==.
}
I'm getting the error "variable var not found." I've been following the syntax in this documentation and also referred to this example, though my variables all have different names and can't be easily renamed, so it's important that I'm able to specify a list of distinct variable names in the loop.
I've tried renaming the variables to be standard format (ex. geovar1, geovar2...etc) and am getting the error "variable geo_vars not found" from the following code:
local geo_vars region state city
local n = 1
foreach var of varlist geo_vars {
ren var geovar`n'
local `n' = n+1
}
foreach var of varlist geo1-geo10 {
bysort household_id: replace var = var[1] if var==.
}
This leads me to believe that I'm having a syntax error in my loop construction. I welcome any feedback or suggestions you may have. Thank you.

Comment