Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: local r(varlist)?


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: local r(varlist)?
Date   Sun, 15 Jun 2008 18:21:15 +0100

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 
n.j.cox@durham.ac.uk 

Austin Nichols

Keith Dear <keith.dear@anu.edu.au>:
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
<martin.weiss@uni-tuebingen.de> wrote:
> See -h foreach- to note that you must say -in- with -anything-
<snip>
>
> Zitat von Keith Dear <keith.dear@anu.edu.au>:
>
>> 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/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index