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

RE: st: Loop trouble


From   Nick Cox <[email protected]>
To   [email protected]
Subject   RE: st: Loop trouble
Date   Fri, 09 May 2003 18:22:34 +0100

-------------------
> Nick,
> 
> I was sloppy in the e-mail, not in the original code. Also,
initially I used
> 
> 
> forvalues i=1/`x'
> 
> to no avail. So I switched to the programming manual's less elegant
> 
> forvalues i=1(1)`x'
> 
> out of desperation. 
> 
> Anyway, here's a strange ambiguity:
> 
> Suppose mytempfile has 17 observations. The code
> 
> use "`mytempfile'"
> display _N
> local x _N
> display `x'
> 
> will have the same effect as
> 
> use "`mytempfile'"
> display _N
> local x=_N
> display `x'
> 
> The effect is that the display command will twice show the right
number, 17,
> in both cases.
> 
> The difference between two slabs of code is that
> 
> forvalues i=1/`x' 
> 
> does not work after the first, but works fine after the second. 
> 
> In other words, the equal sign makes no difference to the value of
the local
> variable x, yet it does matter to forvalues.
> 

_N is strange. It is not a variable, or a macro. There are, however,
contexts in which it is evaluated automatically, and in effect it
behaves in those contexts as it were a macro -- or even a variable.
That is, Stata sees _N and replaces it by the appropriate (or
numbers!). 

-display- is one example. -display- is prepared to do some 
work for you, and arguments to -display- which are expressions are
evaluated, Indeed -display- has a major role as a calculator. 

-forvalues- is at the other extreme. _N is just not 
accepted as an argument, as you found. 

Yet another way to do it in Stata 8 is

forval i = 1 / `c(N)' { 
       ....
} 

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