Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

From |
Maria Ana Vitorino <vitorino@wharton.upenn.edu> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
Re: st: macro of macros? |

Date |
Mon, 7 Nov 2011 10:06:31 -0500 |

Thank you all for such detailed explanations! Best, Ana On Nov 7, 2011, at 10:02 AM, Nick Cox wrote:

Billy has good advice which repeats some already given in this thread, but his initial statement deserves a challenge. 1. Stata has fair support for matrices and very good support for matrices through Mata. Those are _major_ exceptions to the generalisation in the first sentence. 2. Billy is right that attacking this problem through OOP would be clunky in Stata. But it would be clunky in almost any language and crazy in most to set up code for sets of macros in such terms.On Mon, Nov 7, 2011 at 2:52 PM, Billy Schwartz<wkschwartz@gmail.com> wrote:Ana, Fundamentally, Stata does not make it easy to use data structuresother than the dataset and macros. Technically, it supports OOP,whichyou would use in most other languages to solve this kind of problem, but the syntax is so clunky, you would never write a Stata class unless there's no other way. Here's my suggestion for solving this problem using macros. Each 'set#' macro you create is like an array, and you want an arrayof these arrays, which you'll call 'sets'. You'll use the # at theendof each macro name 'set#' as the index within 'sets'. Name these macros set1 ... setN WITH NO SKIPS, so if you need to remove set 4, you'll take the contents of setN and put it in set4 and delete setN. You'll use "double indirection", a macro with in a macro, to substitute in the # in the loops. `set1' becomes pointer = 1; `set`pointer''. Notice the nested macro expansion operators. local set1 x1 x2 local set2 x2 x3 local set3 x1 x3 //the following block is like your local sets ""`set'*"" local pointer = 1 while ("`set`pointer++''" != "") { /*help macro (the ++ AFTERthe pointer macro name increments the pointer macro AFTER the macro is expanded)*/ local sets `sets' set`pointer' }local --pointer //decrements the pointer to point to the lastpositionin the sets array. only really necessary if you use version 2 below //version 1 foreach xvars of local sets { regress y ``xvars'' //notice the double indirection used here } //version 2. not necessarily better or worse than version 1 forvalues i = 1/`pointer' { regress y `set`i'' //notice double indirection here } On Sun, Nov 6, 2011 at 10:01 AM, Maria Ana Vitorino <vitorino@wharton.upenn.edu> wrote:Dear statalist users,I've only recently started experimenting with macros and I havethe following question: can we have a macro of macros and loopthrough the different sub-macros without having to set beforehandhow many sub-macros there are in the macro? Maybe it's easier tounderstand what I'm looking for with an example:I know that the following can be done: local set1 "x1 x2" local set2 "x2 x3" local sets ""`set1'" "`set2'"" *** foreach xvars of local sets { reg y xvars }But, instead of having to list all the macros in the line ***, canwe have something like:local set1 "x1 x2" local set2 "x2 x3" local sets ""`set'*"" foreach xvars of local sets { reg y xvars }Ideally I would like to add (or remove) sets as a please so Iwouldn't like to have to keep updating the line *** everytime I doso...* * 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/

* * 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/

**References**:**st: macro of macros?***From:*Maria Ana Vitorino <vitorino@wharton.upenn.edu>

**Re: st: macro of macros?***From:*Billy Schwartz <wkschwartz@gmail.com>

**Re: st: macro of macros?***From:*Nick Cox <njcoxstata@gmail.com>

- Prev by Date:
**Re: st: macro of macros?** - Next by Date:
**Re: st: macro of macros?** - Previous by thread:
**Re: st: macro of macros?** - Next by thread:
**Re: st: macro of macros?** - Index(es):