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

st: RE: Re: Problem with foreach


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Re: Problem with foreach
Date   Thu, 29 Jul 2004 22:41:55 +0100

I have a different comment. I can't see what you want to 
achieve by creating a variable with integers running from 
1 to _N. To see this: I just did the -gen- and -codebook- for a dataset 
I have in memory, but all the information can be constructed
without knowing anything but _N. It says nothing about the 
particular data. 

. codebook id

--------------------------------------------------------------------------------
id                                                                   (unlabeled)
--------------------------------------------------------------------------------

                  type:  numeric (float)

                 range:  [1,40]                       units:  1
         unique values:  40                       missing .:  0/40

                  mean:      20.5
              std. dev:   11.6905

           percentiles:        10%       25%       50%       75%       90%
                               4.5      10.5      20.5      30.5      36.5


I may be missing something here, but if you want to know how many observations
you have, you can (e.g.) 

. count 

or even look at the file without reading it in. 
So you can go something like 

local country "Benin Togo France"  
foreach i of local country { 
	qui d using `i' 
	di "`i'" _col(20) `r(N)' 
} 

Or perhaps this was just a toy example, and nothing to do with 
the real problem. 
 
Nick 
[email protected] 

> 
> --- [email protected] wrote:
> > I have a list of countries. I put them in a macro.
> > When I run the loop, it works fine if I only ask for
> > basic tasks such as describe or su.
> > But when I try to do a bit more complicated tasks,
> > nothing appeared. It proceeds only for the first country.
> > And the remaining is blank.
> >
> > [...]
> > 
> > But when I code:
> > local country "Benin Togo France"
> > foreach i of local country {
> >  use `i'.dta
> >  di "I use `i'"
> >  g id = _n
> >  codebook id
> > }
> > 
> > It works only for the first country: Benin.
> 

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