Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Austin Nichols <austinnichols@gmail.com> |
To | statalist@hsphsun2.harvard.edu |
Subject | Re: st: RE: macro of macros? |
Date | Sun, 6 Nov 2011 13:16:09 -0500 |
Nick-- The claim that [With the second solution] "First" and "part" would be taken as separate words is not true, but I was not addressing any particular aspect of Maria Ana Vitorino's problem, but merely illustrating approaches in the vein of how to use a single local to accumulate a long string or collection of quote-delimited strings. *Note that loc o First part loc o `" "`o'" "second part" "' loc o `" `o' "third part" "' di `"`o'"' foreach part of local o { di `"`part'"' } *is the same as loc o "First part" loc o `" `o' "second part" "' loc o `" `o' "third part" "' di `"`o'"' foreach part of local o { di `"`part'"' } On Sun, Nov 6, 2011 at 11:37 AM, Nick Cox <n.j.cox@durham.ac.uk> wrote: > The first solution raises the question of how it is to be parsed later. > > With the second solution when Ana comes to go > > foreach part of local o { > > } > > "First" and "part" would be taken as separate words. > > Nick > n.j.cox@durham.ac.uk > > Austin Nichols > > Maria Ana Vitorino <vitorino@wharton.upenn.edu>: > I, too, prefer code I can cut and paste in one block into the command > window, so I use only * comments and collect long lines in macros; it > is easy enough to do something like: > > loc o First part > loc o `o', second part > loc o `o', third part > di "`o'" > > or > > loc o First part > loc o `" "`o'" "second part" "' > loc o `" `o' "third part" "' > di `"`o'"' > > On Sun, Nov 6, 2011 at 11:15 AM, Maria Ana Vitorino > <vitorino@wharton.upenn.edu> wrote: >> thanks. That is indeed a simple solution to this problem. I usually try to >> avoid using #delimit because I like to paste parts of the code into the >> command window directly and that cannot be done when #delimit is used. But >> in this case there may not be a way around it... >> Thanks again, >> Ana >> >> On Nov 6, 2011, at 10:52 AM, Nick Cox wrote: >> >>> That strikes me as being a question about laying out your code. You can >>> use multiline definitions in conjunction with #delimit ; . >>> >>> Nick >>> n.j.cox@durham.ac.uk >>> >>> Maria Ana Vitorino >>> >>> ok. so maybe I wasn't clear.... >>> >>> What Tirthankar suggested (which is below) works fine but the problem >>> is that it's not very easy to read what are the different sets used in >>> the estimation, i.e. the first line inTirthankar's suggested code can >>> get very long and hard to read if one has many different >>> specifications with many variables. >>> So, what I was trying to do was to define the different sets in >>> separate lines so that it's easier to read and make changes. What you >>> proposed in the previous response works well but may be prone to >>> errors so I was wondering if there was a way around that. >>> Is it more clear what I'm looking for now? Any help is appreciated. >>> >>> Tirthankar's suggestion: >>> >>> local rhssets ""x1 x2" "x4 x5" "x2 x6"" >>> local counter = 1 >>> >>> foreach x of local rhssets { >>> reg y `x' >>> predict yhat`counter' >>> local counter = `counter' +1 >>> } >>> >>> Your suggestion: >>> local index >>> local set1 "x1 x2" >>> local index `index' 1 >>> local set2 "x2 x3" >>> local index `index' 2 >>> >>> foreach i of local index { >>> reg y xvars `set`i'' >>> } >>> >>> Ana >>> >>> On Nov 6, 2011, at 10:29 AM, Nick Cox wrote: >>> >>>> You can do what Tirthankar showed you just recently. In many ways >>>> it's a better method. For reasons that weren't clear to me it seemed >>>> that you wanted something different. >>>> >>>> You might find these tutorials useful: >>>> >>>> SJ-3-2 pr0009 . . . . . . . . . . . . . Speaking Stata: Problems >>>> with lists >>>> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . >>>> N. J. Cox >>>> Q2/03 SJ 3(2):185--202 (no >>>> commands) >>>> discusses ways of working through lists held in macros >>>> >>>> SJ-2-2 pr0005 . . . . . . Speaking Stata: How to face lists with >>>> fortitude >>>> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . >>>> N. J. Cox >>>> Q2/02 SJ 2(2):202--222 (no >>>> commands) >>>> demonstrates the usefulness of for, foreach, forvalues, and >>>> local macros for interactive (non programming) tasks >>>> >>>> Nick >>>> n.j.cox@durham.ac.uk >>>> >>>> Maria Ana Vitorino >>>> >>>> Thanks Nick. This is very helpful. >>>> This requires that every time I add a set I have to include two >>>> additional lines and to make sure that the name of the set is in line >>>> with the index. Using the example again, >>>> >>>> If I add another set, say set 4 I need to do: >>>> >>>> local set4 "x5 x6" >>>> local index `index' 4 >>>> >>>> but suppose that what I do (by mistake) is >>>> >>>> local set4 "x5 x6" >>>> local index `index' 3 >>>> >>>> Any chance I can add another set in such a way that these types of >>>> mistakes won't happen? >>>> Thanks! >>>> Ana >>>> >>>> >>>> On Nov 6, 2011, at 10:07 AM, Nick Cox wrote: >>>> >>>>> Wildcards are for variable names only. But you don't need any such >>>>> device here. There are various ways to approach what you want. >>>>> Here's one: >>>>> >>>>> local index >>>>> local set1 "x1 x2" >>>>> local index `index' 1 >>>>> local set2 "x2 x3" >>>>> local index `index' 2 >>>>> >>>>> foreach i of local index { >>>>> reg y xvars `set`i'' >>>>> } >>>>> >>>>> Nick >>>>> n.j.cox@durham.ac.uk >>>>> >>>>> Maria Ana Vitorino >>>>> >>>>> 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/