I have local variables x1 , x2, and x3
as follows
local x1 2 3 5
local x2 5 9 7
local x3 1 3 4
Now I define local x
as
local x `x1' `x2' `x3'
Next, I define for loop as
foreach var of varlist `x'{
reg y `var'}
The problem is that stata is giving me the error (note y
is dependent variable)
invalid name
Any suggestion in this regard will be highly appreciated.
I think of macros as "delayed typing". This is the approach I use.
sysuse auto, clear
local x1 weight
local x2 headroom trunk
local x3 length turn
forvalue i = 1/3 {
regress price `x`i''
}
Assuming these are variables, Richardh's solution would obviously work. However it requires that you rename all your macros even though that's not necessary.
You can just expanding the macros twice:
local x x1 x2 x3
foreach var of local x {
reg y ``var''
}
You could also do this, but you'll have problems if your lists of variables are too long:
local x "`x1'" "`x2'" "`x3'"
foreach var of local x {
reg y `var'
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With