I have a longitudinal data set with recurring observations (id
1,2,3...) per year
. I have thousands of variables of all types. Some rows (indicated by a variable to_interpolate == 1
) need to have their numeric variables linearly interpolated (they are empty) based on values of the same id
from previous and next years.
Since I can't name all variables, I created a varlist
of numeric variables. Also, I do not want to recreate thousands of extra variables, so I need to replace the existing missing values.
What I did so far:
quietly ds, has(type numeric)
local varlist `r(varlist)'
sort id year
foreach var of local varlist {
by id: ipolate `var' year replace(`var') if to_interpolate==1
}
No matter what I do, I get an error message:
factor variables and time-series operators not allowed
r(101);
My questions:
- How is the 'replace' even proper syntax? if not, how to replace the existing variable values instead of creating new variables?
- If the error means that factors exist in my varlist - how to detect them?
- If not, how to get around this?
Thanks!
r(varlist)
andvar
being given as for example'var'
where they should be given as`var'
- the leftmost character is the so-called "left single quote" character which appears on my keyboard in the upper left corner beneath the tilde character. (It is technically the ASCII "accent grave" character.) – user4690969ipolate
command does not include areplace()
option. So do it in 3 commands. (1) useipolate `var' ...
to generate a new variable named temp; (2)replace `var' = temp
(3)drop temp
. – user4690969