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
Matthew White <[email protected]>
To
[email protected]
Subject
Re: st: macro of macros?
Date
Mon, 7 Nov 2011 10:00:07 -0500
> 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'> }
I think you want the increment at a later point. On the first
iteration of the loop, you'll get
local sets `sets' set2
since
while ("`set`pointer++''" != "")
increments `pointer' from 1 to 2.
How about:
local pointer = 1
while ("`set`pointer''" != "") {
local sets `sets' set`pointer++'
}
Best,
Matt
On Mon, Nov 7, 2011 at 9:52 AM, 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...
>>
>> Any suggestions are appreciated.
>>
>> thanks,
>> Ana
>> *
>> * 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/
>
--
Matthew White
Data Coordinator
Innovations for Poverty Action
101 Whitney Avenue, New Haven, CT 06510 USA
+1 434-305-9861
www.poverty-action.org
*
* 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/