Statalist


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

Re: st: local r(varlist)?


From   "Austin Nichols" <[email protected]>
To   [email protected]
Subject   Re: st: local r(varlist)?
Date   Sat, 14 Jun 2008 12:26:48 -0400

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?
>> kd
*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index