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   Joly.Patrick@ic.gc.ca
To   statalist@hsphsun2.harvard.edu
Subject   st: RE: Help: typo-error in for-loop or more serious problem??
Date   Mon, 24 Feb 2003 10:28:17 -0500

Ernest Berkhout
> i was looking for a way to cycle through all the values of a 
> particular
> variable 'oplnr' without having to hardcode these values (they changes
> every year). Walking through some faqs I come across the 
> program 'vallist'
> which should be able to do this for me. I integrated this in a small

-vallist- is considered by its author to have been superseded by -levels-
(also same author).  type -ssc desc levels-.

> foreach-loop program, but I get an errormessage that does not 
> make sense to
> me. As I'm not very experienced in programming Stata, it might be some
> silly typo which I overlooked, but I cant see what is is. Or 
> is there maybe
> some other problem unknown to me?
>
> (snip)
>
> The following is the code that I've written:
> ******************************
> gen temp=.
> vallist oplnr
> foreach oplnr of local r(list)  {
>         forvalues brinnr = 1/13     {
>                  gen byte opXbr_`oplnr'_`brinnr' = (oplnr==`oplnr' &
> brinkort==`brinnr')
>                  qui summ opXbr_`oplnr'_`brinnr', meanonly
>                  replace temp=r(max)
>                  if temp==0  drop opXbr_`oplnr'_`brinnr'
>         }
> }
> drop temp
> ********************************************
> 
> The error message I get is:
> 
> { required
> r(100)


r(list) is a special kind macro, one that is saved in the r-space which is a
namespace common to rclass programs.  The macros in r(), (or e() and s() for
that matter) are not really local macros, e.g.

use c:\stata\auto
vallist foreign
* show the contents of r(list)
* i.e it expands _like_ a local macro with the single-quotes
di "`r(list)'"
* but doesn't turn up when we list the contents of local and global macros
macro dir
* it does show up when we list the contents of the r-space
return list

In your loop, replace the line

   foreach oplnr of local r(list)

by

   foreach oplnr in `r(list)'

or by the following two lines

   local myrlist = r(list)
   foreach oplnr of local myrlist

which in effect copy the rclass macro r(list) into a (truly) local macro


Patrick Joly
joly.patrick@ic.gc.ca
pat.joly@utoronto.ca
*
*   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