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

RE: st: Loop trouble


From   "Nick Winter" <[email protected]>
To   <[email protected]>
Subject   RE: st: Loop trouble
Date   Fri, 9 May 2003 13:23:12 -0400

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

Following up on Nick Cox's point, in fact, the first and second approach
*DO NOT* put the same thing into the local macro x.  In the first case,
the local macro contains the literal string _N.  In the second case, the
local macro contains the number 17 (or whatver the number of
observatrions happens to be).

In the first case, when you say 

	display `x'

this is substituted to 

	display _N

by stata (because `x' has the value _N), and then the display command
evaluates _N to be 17.

Compare:

	local x _N
	di "`x'"

with

	local x=_N
	di "`x'"

to see what is really in the local macro, without further substitution
by the display command.

Nick Winter


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