Dear statalists,
I am not entirely sure whether "use local in global" is an appropriate description for my problem but here is what I would like to do:
I am running several regressions for different dependent variables. The dependent variables differ in the year they refer to.
First, I would like to define for globals for different control variables. With a foreach loop, I define these for every decadal year between 1950 and 1990. Then, when running the actual regressions, I would like to use these controls but only for the year in the dependent variable.
The result is that I try to call upon a local (from foreach loop) within a global (previously defined). For example,
This does not work. Stata gives the error:
.
Did I miss something obvious? Is this possible at all?
Many thanks,
Milan
I am not entirely sure whether "use local in global" is an appropriate description for my problem but here is what I would like to do:
I am running several regressions for different dependent variables. The dependent variables differ in the year they refer to.
Code:
foreach year of numlist 1950(10)1990 {
global control_dist1_`year' weighted_dist_y`year'
global control_dist2_`year' weighted_dist_y`year' c.weighted_dist_y`year'#c.weighted_dist_y`year'
global control_dist3_`year' weighted_dist_y`year' c.weighted_dist_y`year'#c.weighted_dist_y`year' c.weighted_dist_y`year'#c.weighted_dist_y`year'#c.weighted_dist_y`year'
}
foreach year of numlist 1950(10)1990 {
reg mig_total_`year' weighted_sc_initial10_y`year' $control_dist1_`year' $control_pop_`year', robust // linear dist control
reg mig_total_`year' weighted_sc_initial10_y`year' $control_dist2_`year' $control_pop_`year', robust // quadratic d control
reg mig_total_`year' weighted_sc_initial10_y`year' $control_dist3_`year' $control_pop_`year', robust // cubic dist. control
}
The result is that I try to call upon a local (from foreach loop) within a global (previously defined). For example,
Code:
$control_dist1_`year'
1950 [i.e. the first year in the foreach loop] invalid name
Did I miss something obvious? Is this possible at all?
Many thanks,
Milan

Comment