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: foreach global macro not working with multiple macros


From   Eric Booth <[email protected]>
To   "<[email protected]>" <[email protected]>
Subject   Re: st: foreach global macro not working with multiple macros
Date   Wed, 2 Feb 2011 18:08:50 +0000

<>

Building on one of Nick's suggestions, if your globals aren't systematically named, you could write the -macro li- to a log file and extract the list from that:

*******  Grabbing list of all macros that start with "c"
global cash "pocket asdf bank"
global check "bank"
global c3 "blah blah"
global c99 "test"
global z1 "ignore this one"


*******get names of all globals
preserve
 cap log close _all
 log using "macros.txt", text replace
 macro li
 log close _all
 clear
 insheet using macros.txt, tab nonames
**first 4 and last 6 lines are from log, remove those: 
 drop in 1/4
 drop in `=_N-6'/l
**keep if first letter is c
** (check this result, it may contain extra/unwanted macros)
 tempvar flag
 g `flag' = 1 if substr(v1, 1, 1)=="c"
 drop if `flag' != 1
**get names of all globals that start with "c"**
 split v1, p(": ")
 keep v11
 levelsof v11, loc(all) clean
 global all `all'
 di `"$all"'
restore
********


foreach y of global all  {
display "`y'"
}
****

- Eric

__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
[email protected]

On Feb 2, 2011, at 11:58 AM, Nick Cox wrote:

> You can't wildcard anything other than variable names to my knowledge. 
> 
> The use of macros, even though you can use only one at a time, is that you can put everything in it. It's like being able to use an indefinitely extensive bag for all your shopping. 
> 
> If you go 
> 
> . mac li 
> 
> you can see all your globals. Copy and paste from your Results windows into your do-file editor and hack around and you can get a list of names. Suppose you have globals p1, p2, p3, ..., p200. You can do this 
> 
> foreach i in num 1/200 { 
> 	... $p`i' 
> }
> 
> That is, you can delay the global reference until inside the loop. 
> 
> There may well be other ways to do it. 
> 
> What are you using globals for any way? I don't use globals from one month to the next. That's partly taste. It's not for lack of using Stata. 
> 
> Nick 
> [email protected] 
> 
> Nick Mosely
> 
> Thanks, guys. That does work, Eric. Unfortunately I have about 100
> macros that I would like to loop over. Their names all start with the
> letter p, so I was hoping to do something like the following:
> 
> foreach y of global p* {
> 
> I wonder, what is the use of -foreach- with macros if you can only use
> one at a time?
> I guess I'll have to find another way.
> 
> On Wed, Feb 2, 2011 at 9:25 AM, Eric Booth <[email protected]> wrote:
> 
>> I also find it useful to combine the global macros before the loop (esp. if there's repetition in the elements), e.g.
>> 
>> **************!
>> global cash "pocket asdf bank"
>> global check "bank"
>> **
>> global all: list global(cash) | global(check)
>> di `"${all}"'
>> **
>> foreach y of global all {
>> display "`y'"
>> }
>> **************!
> 
> On Feb 2, 2011, at 11:19 AM, Eric Booth wrote:
> 
>>> So you could change it to:
>>> 
>>> *****
>>>> global cash "pocket"
>>>> global check "bank"
>>>> foreach y in $cash $check {
>>>> display "`y'"
>>>> }
>>> *****
>>> 
>>> to get it to work.
>>> 
>>> On Feb 2, 2011, at 11:15 AM, Maarten buis wrote:
>>> 
>>>> --- On Wed, 2/2/11, Nick Mosely wrote:
>>>>> I would like to create a for loop using global macros but
>>>>> Stata continues to return an error message.
>>>>> 
>>>>> This code works fine:
>>>>> 
>>>>> global cash "pocket"
>>>>> foreach y of global cash {
>>>>> display "`y'"
>>>>> }
>>>>> 
>>>>> The following, code on the other hand, does not:
>>>>> 
>>>>> global cash "pocket"
>>>>> global check "bank"
>>>>> foreach y of global cash check {
>>>>> display "`y'"
>>>>> }
>>>> 
>>>> In -foreach- of local / of global you can specify only
>>>> one name of a local or global macro. You specified two.
> 



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