Reputation: 1
I have two foreach
loops:
is tightening instruments[ 1A, 2A, 3A, 4A]
is loosening measures [ 1b, 2b, 3b, 4b] (this is not same exact code).
My regression should be running with 1 tightening and 1 loosening measure at the same time. I tried including two loops but that's running one measure at a time. How do I get it to run one variable of each list at the same time (an overall of four regressions)?
local MPPTD "MPP_TD CAP_TD LIQ_TD CRE_TD"
local MPPLD "MPP_LD CAP_LD LIQ_LD CRE_LD"
foreach instrument of local MPPTD {
foreach measure of local MPPLD {
xtreg `dep'`i' drate `instrument' `measure' interaction`instrument' ///
interaction`measure' `rhsvar', fe
Upvotes: 0
Views: 83
Reputation: 82
Nick's answer is spot on as usual. Extended macro functions are key. For something even more general, I like an implementation like the following:
local MPPTD "MPP_TD CAP_TD LIQ_TD CRE_TD"
local MPPLD "MPP_LD CAP_LD LIQ_LD CRE_LD"
assert `:list sizeof MPPTD' == `:list sizeof MPPLD'
// The above line ensures the 2 locals have the same num of elements
forvalues i = 1/`:list sizeof MPPTD' {
local instrument: word `i' of `MPPTD'
local measure: word `i' of `MPPLD'
xtreg `dep'`i' drate `instrument' `measure' interaction`instrument' ///
interaction`measure' `rhsvar', fe
}
Upvotes: 1
Reputation: 37183
Your desired loop is over four cases, so one loop not two nested loops. In general, you might do something like
local Alist frog toad newt dragon
local Blist Harry Hermione Ron Neville
forval j = 1/4 {
* code in terms of word `j' of `Alist' and word `j' of `Blist'
}
and there are other ways to do it.
In your case there is simple structure that exists to be exploited:
local prefix MPP CAP LI CRE
foreach pre of local prefix {
* code in terms of `pre'LD and `pre'TD
}
and I can see no objection to
foreach pre in MPP CAP LI CRE {
Upvotes: 2