An alternate take on the problem, building on Jeph's suggestion:
x-------------Begin code------------x
* Create some test local macros
local number 123
local string ABC
local quotes "`"This was compound quoted"'"
local lsquote left single ` quote
* User should modify the following path and filename
* to accommodate their own needs
local mac_envir `c(sysdir_personal)'macs.doh
*------------- save locals ------------------
tempfile _macs macs
qui log using `_macs', text replace
macro list
qui log close
tempname in out
filefilter `"`_macs'"' `"`macs'"', from(\LQ) to("\BS`") // left quotes fixed
* view `"`macs'"' // uncomment this line if you want to see the input file
file open `in' using `"`macs'"', read
file open `out' using `"`mac_envir'"', replace write
file read `in' line
while !r(eof) {
if regexm(`"`macval(line)'"',"^\_([A-Za-z1-9_]+):(.*)") {
if !inlist(`"`=regexs(1)'"',"_macs","macs","mac_envir") {
file write `out' `"local `=regexs(1)' `=regexs(2)'"' _n
}
}
file read `in' line
}
file close `in'
file close `out'
*----------- done --------------------------
mac drop _all
* The following include should mirror the
* path and filename specified above
include `c(sysdir_personal)'macs.doh
mac dir
x-------------End code------------x
DCE
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/