Bookmark and Share

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]

Re: st: macro of macros?


From   Billy Schwartz <[email protected]>
To   [email protected]
Subject   Re: st: macro of macros?
Date   Mon, 7 Nov 2011 10:46:10 -0500

Nick,

Fair points. Is there a way to do what Ana was looking to do using
matrices, treating them as arrays of arrays perhaps?


On Mon, Nov 7, 2011 at 10:02 AM, Nick Cox <[email protected]> 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 <[email protected]> wrote:
> > Ana,
> >
> > Fundamentally, Stata does not make it easy to use data structures
> > other than the dataset and macros. Technically, it supports OOP, which
> > you 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 array
> > of these arrays, which you'll call 'sets'. You'll use the # at the end
> > of 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 last position
> > in 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
> > <[email protected]> wrote:
> >>
> >> Dear statalist users,
> >> I've only recently started experimenting with macros and I have the following question: can we have a macro of macros and loop through the different sub-macros without having to set beforehand  how many sub-macros there are in the macro? Maybe it's easier to understand 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 ***, can we 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 I wouldn't like to have to keep updating the line *** everytime I do so...
> >>
>
> *
> *   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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index