Reputation: 372
My problem appears in a more general setting but can be illustrated with this example: the following lines create two new variables saving the estimates for a regressor using two different estimators
sysuse auto, clear
reg price mpg, r
local m "reg"
gen bmpg_`m' = _b[mpg]
label var bmpg_`m' "`m' estimate"
areg price mpg, absorb(foreign) r
local m "areg"
gen bmpg_`m' = _b[mpg]
label var bmpg_`m' "`m' estimate"
To save space and avoid repetitions, I created a external do-file, called savest.do
which stores the following repeated two lines:
gen bmpg_`m' = _b[mpg]
label var bmpg_`m' "`m' estimate"
So, I get a shorter program:
sysuse auto, clear
reg price mpg, r
local m "reg"
do savest
areg price mpg, absorb(foreign) r
local m "areg"
do savest
However, this shorter program fails because it doesn't account for the macro m
defined in a different external do-file. I used global
instead of local
but without success.
Upvotes: 1
Views: 94
Reputation: 37208
You can also pass arguments to do-files:
* savest_1.do
args m
gen bmpg_`m' = _b[mpg]
label var bmpg_`m' "`m' estimate"
* savest_2.do
local m `1'
gen bmpg_`m' = _b[mpg]
label var bmpg_`m' "`m' estimate"
reg price mpg, r
do savest_1 reg
Upvotes: 2
Reputation: 9460
The solution is to write a little program that takes the name as an argument:
capture program drop savest
program define savest
syntax namelist(min=1 max=1)
gen bmpg_`namelist' = _b[mpg]
label var bmpg_`namelist' "`namelist' estimate"
end
sysuse auto, clear
reg price mpg, r
savest reg
areg price mpg, absorb(foreign) r
savest areg
Upvotes: 3