'Replicate interpolation for multiple variables
I am interpolating multiple variables in Stata, but don't know how to do it efficiently.
My data looks like this
| Country | Year | v_country_1 | v_country_2 | v_country_3 | ... | v_country_234 |
|---|---|---|---|---|---|---|
| Canada | 2000 | 1005 | 1051 | 1052 | --- | 1120 |
| Canada | 2001 | --- | ||||
| Canada | 2002 | --- | ||||
| Canada | 2003 | --- | ||||
| Canada | 2004 | 2000 | 3500 | 500 | --- | 10562 |
| Korea | 2000 | 1005 | 1051 | 1052 | --- | 1120 |
| Korea | 2001 | --- | ||||
| Korea | 2002 | --- | ||||
| Korea | 2003 | --- | ||||
| Korea | 2004 | 2000 | 3500 | 500 | --- | 10562 |
| ... | ... | ... | ... | ... | --- | .... |
| Uganda | 2000 | 1005 | 1051 | 1052 | --- | 1120 |
| Uganda | 2001 | --- | ||||
| Uganda | 2002 | --- | ||||
| Uganda | 2003 | --- | ||||
| Uganda | 2004 | 2000 | 3500 | 500 | --- | 10562 |
As you can see my data includes multiple countries. So, I need to interpolate the variables by country.
I can interpolate the one variable very easily by using the code :
by cow: ipolate v_country_1 year, gen(v_country_1_ipo)
But, I have 234 variables... so it is almost impossible to replicate this work by hand.
Could you please teach me how to do it? (I know there is a command foreach that is meant to do replication.)
Solution 1:[1]
ds v_country_*
foreach x in `r(varlist)' {
by country: ipolate `x' year, gen(`x'_ipo)
}
// OR
forvalues i = 1/234 {
by country: ipolate v_country_`i' year, gen(v_country_`i'_ipo)
}
Check out help ds and help forvalues or help foreach for further details.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Cybernike |
