Skip to main content

Unabbreviate Macro Lists in Stata

This Statalist thread from a few months ago started by Nick Mosely asked about working with hundreds of macros and eventually got onto the topic of expanding or unabbreviating (see -help unab- for the varlist version of this idea) macro lists.  Based on my posts in that thread, I recently posted -mac_unab- to the SSC Archives to help with this problem.

-mac_unab- is still a bit of a kludge solution, but I haven't figured out a better approach (nor did anyone suggest a better approach).  The biggest issues with mac_unab, which I hope to find better solutions for, include:

1.  When you run mac_unab, it will print all the contents of the -macro list- command in the Results window.  This might be desirable for some, but I'd like to be able to toggle it on/off.  Currently, the way I've gathered the macros is via a log, so there's no way to avoid printing the -mac list- output each time -mac_unab- is run.
2.  Currently, the program will only match macros with the pattern stub*, so you specify what the macros begin with and an asterisk to indicate that you want to match everything with any letters following that prefix.  I'd like to expand those capabilities to match macros based on more complex matching rules like those in -help varlist-, such as  *mymacro*, my?macro, my~macro, etc.  Regardless, the names of your macros will need to be systematic to take advantage of mac_unab, but I'd like to relax the formatting requirements necessary to match of macro names.
The syntax follows closely to -unab- for unabbreviating varlists.


global one "pocket asdf bank"
global two "bank"
global three "blah blah"
global only "end of list"
mac_unab mymacli1 : t*
di "`r(mac_list)'"
di `"$mymacli1"'

mac_unab mymacli2 : on* th*
di `"$mymacli2"'

mac_unab mymacli3 : o* S*, ignore(S_level)
di `"$mymacli3"'

tr: mac_unab mymacli4 : o* S*, ignore(SYSTEM)
di `"$mymacli4"'

**here are the function key mappings:
mac_unab keys: F*
di "$keys"

**end example


  1. Mata's -st_dir()- might help?

  2. I just learned about the all extended macro function, which can return a list of global, scalar, or matrix names. (It looks like it doesn't work with locals.)

  3. Keep on working, great job!

    my site ... panic attack


Post a Comment