Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: Help: typo-error in for-loop or more serious problem??


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Help: typo-error in for-loop or more serious problem??
Date   Mon, 24 Feb 2003 17:09:24 -0000

Ernest Berkhout

> vallist oplnr
> foreach oplnr of local r(list)  {

... 
> 
> The error message I get is:
> 
> { required
> r(100)

Solutions have been suggested, but 
let's back-track to why this doesn't work, 
as the explanation may be of interest. 

Here's my reconstruction. 

-vallist- leaves behind r(list). 

When you type 

. foreach oplnr of local r(list) { 

Stata tries to make sense of r(list) 
as the _name_ of a local macro. It 
sees first 

r 

which could be all or part of a legal name for 
a local macro. So far, so good. Then it sees 

( 

which is definitely not acceptable as part 
of the name of a local macro. So it guesses 
that the macroname has finished and (apart 
from the possibility of spaces) the only legal 
thing it now expects is a "{": manifestly "(" 
does not qualify, so at this point Stata exits and 
emits its error message, as your syntax is illegal. In other words, 
Stata got as far as 

. foreach oplnr of local r( 

before exiting. It didn't try to look further
than that, because it knew this could not possibly 
be right. 

This is all a consequence of the keyword -local-. 

As Patrick explained, if you want to use the 
_value_ of r(list), not its name, you can use 
the syntax `r(list)' but you must go for the 
-in- syntax of -foreach- which spells out
the _in_dividual members of the list. 

. foreach v in `r(list)' { 

or something similar. Sometimes a bare 

r(list) 

will be evaluated by Stata to its 
value, but usually (always???) that is 
to the right of an = sign. 

Nick 
[email protected] 

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