Dears Statalisters,
I am facing a problem with a loop.
I want to create a variable over three decades in a loop, that must be indexed.
Basically, var8 must be associated with 1990, var9 with 2000 and var10 with 2010.
.
How can I do that?
This is my previous code:
The code is wrong because after having generate var_8 for 1990 and saved the file, it starts again the loop and calculate var_9 for the same year and ovewwrite the old file.
I am facing a problem with a loop.
I want to create a variable over three decades in a loop, that must be indexed.
Basically, var8 must be associated with 1990, var9 with 2000 and var10 with 2010.
.
How can I do that?
This is my previous code:
Code:
clear all
foreach y of numlist 1990 2000 2010{
forvalues i=8/10{
use data.dta, clear
keep if year==`y'
gen county_`y' = statefip*10000+countyicp
fcollapse (sum) a`y' b`y' c`y' d`y', by(f_`y' year county_`y')
gen var_`i' = f``y'*c`y'
save data`y', replace
}
}
The code is wrong because after having generate var_8 for 1990 and saved the file, it starts again the loop and calculate var_9 for the same year and ovewwrite the old file.

Comment