Braces are also necessary when the global name contains a local macro...
. local loc a
. global ga hello world
. di "$ga"
hello world
. di "$g`loc'"
a
. di "${g`loc'}"
hello world
This was the case in the posted query... as I said in my previous response (which doesn't seem to have arrived yet more than an hour since I sent it) the -foreach x of global g- construct solves the problem.
D
-----Original Message-----
From: Michael Blasnik [mailto:[email protected]]
Sent: 11 April 2005 12:21
To: [email protected]
Subject: Re: st: foreach and globals and {.}
The ${} are used to make the name of a global macro explicit to Stata when
the context would otherwise make it unclear -- primarily when you want to
embed a global macro name in a text string:
di "abc${myglobal}def"
allows Stata to figure out that the global macro is called myglobal. In the
original pos, they don't need that syntax.
Michael Blasnik
[email protected]
----- Original Message -----
From: "Ulrich Kohler" <[email protected]>
To: <[email protected]>
Sent: Monday, April 11, 2005 7:04 AM
Subject: Re: st: foreach and globals and {.}
> Hans J. Baumgartner wrote:
>> Dear statalist,
>>
>> has anybody an idea how to solve the following problem?
>>
>>
>> foreach x in ${out_`year’} {
>> do something
>> }
>>
>> That is: I have several globals and I need the braces to tell Stata that
>> the local `year’ actually belongs to the global name.
>>
>> Now I would like to run a loop over all elements in the global
>> ${out_`year’}.
>>
>> But the foreach command gets puzzled with the braces around the global,
>> since it thinks that this means to start the loop. And this returns an
>> error that a code follows on the same line as open braces.
>
> I don't understand why you need the braces in ${out_`year'}. If
> $out_`year'
> is a string, consisting out of several elements,
>
> foreach x in $out_`year' {
> do something
> }
>
> would be fine. If $out_`year' refers to a single element, which changes
> according to `year' within a loop, I cannot see why you start a loop at
> all.
>
> (I must admit that I have never seen something like ${out_`year'}, and I
> was
> kind of surprised that
>
> . global x "test"
> . di "${x}"
> test
>
> works. Can anybody tell me what that means?)
>
> uli
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/