To expand slightly:
I think of it this way:
r(varlist) is a local macro in the wide sense but not in the strict
sense.
`r(varlist)' is in effect a request: treat r(varlist) as if it were a
local macro.
The error message from
>> . foreach v of local r(varlist) {
>> (error message: "{ required")
is not transparent here, but it is a side-effect of Stata when it reads
your command line not understanding your syntax. The parser won't do
evaluations on the fly unless so
instructed, which is why you need here to spell out using the ` '
notation that you want the contents of the "macro" to be visible to
-foreach-.
Nick
[email protected]
Austin Nichols
Keith Dear <[email protected]>:
Martin Weiss's claim about -foreach- is incorrect; you can use either
-in- or -of any- with -foreach- to mean the same thing, though I
believe the -of any- syntax is undocumented.
More to the point, r(varlist) is not the name of a local macro, so it
cannot serve this role:
foreach v of local r(varlist) {
though I do think the error message should be more informative there.
The `r(varlist)' trick for accessing returned results (or system
parameters etc.) inline is equivalent to `: di r(varlist)' for most
purposes (with only one exception I can think of off the top of my
head) which is equivalent to a two-step approach defining a new local
macro, i.e. these three -foreach- loops are equivalent:
ds
foreach v in `r(varlist)' {
di "`: var l `v''"
}
local r: di r(varlist)
foreach v in `r' {
di "`: var l `v''"
}
foreach v of local r {
di "`: var l `v''"
}
*but this is not equivalent:
local r=r(varlist)
foreach v in `r' {
di "`: var l `v''"
}
(though it will give equivalent results in many cases) since the equal
sign truncates the result at about 245 characters.
See -help extended_fcn- and [P] macro for more info.
On Sat, Jun 14, 2008 at 11:05 AM, Martin Weiss
<[email protected]> wrote:
> See -h foreach- to note that you must say -in- with -anything-
<snip>
>
> Zitat von Keith Dear <[email protected]>:
>
>> This is a small point I am puzzled by, but in the interests of
knowing
>> how Stata thinks:
>>
>> Following -ds- (and I guess many other commands),
>>
>> this works:
>> . foreach v of any `r(varlist)' {
>> (so r(varlist) is behaving like a local macro)
>>
>> but this doesn't:
>> . foreach v of local r(varlist) {
>> (error message: "{ required")
>>
>> what am I misunderstanding here?
*
* 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/