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

st: RE: using scalars in a loop


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: using scalars in a loop
Date   Wed, 12 Nov 2003 10:23:58 -0000

Short answer: I don't know. 

My guess is this: 

-forvalues- expects to see a number at that 
position. (-while- on the other hand is 
willing to do more work for you.) 

This code works if there is no name conflict, i.e. 
no variable unambiguously abbreviatable [?] 
as -n-:  

forval i = 1/`=n' { 
	...
} 

and this works if there is a name conflict: 

forval i = 1/`=scalar(n)' { 
	...
} 

Note that these are not counterexamples 
to your assertion that -forval- doesn't 
work with scalars. In these examples, 
-forval- never sees the scalar; it just 
sees the numerical result of evaluating 
it in place. The scalar is out of sight, 
and so out of mind. 

It is the same principle, I think, 
with locals. Why does 

local n = _N 
forval i = 1/`n' { 
	...
} 

work? Because an important thing that
Stata does when interpreting your 
code is to replace all local and global 
macros with their current contents _before_
passing the code to the command in question. So 
`n' is _never_ seen as such by -forval-. 
It just sees the numerical (in this 
case) result of substituting its contents
in place. 

Aside: You don't need a loop of any kind 
for this example. 

gen ind = index(var1, "r") > 0 

gets you there in one. 

Nick 
n.j.cox@durham.ac.uk 

Dimitriy V. Masterov
 
> I am curious why I cannot use a scalar in a forvalues loop. 
> I was trying
> to write a loop that will create a variable ind that equals 
> 1 if the var1
> contains the letter "r", 0 otherwise. I suspect it has 
> something to do
> with the frontslash in the second example, but I have not 
> found a way to
> get around it. Some sentences about the backslash in [U] 
> 21.3.9 hint that
> this might be the problem, though I don't know for sure.
> 
> The code below works fine to do what I want:
> **********************************************************
> local n=_N
> gen ind=0
> forvalues v=1/`n' {
>         replace ind=index(var1[`v'],"r") in `v'
> }
> replace ind=1 if (ind>1 & ind !=.)
> ***********************************************************
> 
> 
> while this code does not work:
> ***********************************************
> scalar n=_N
> gen ind=0
> forvalues v=1/n {
>         replace ind=index(var1[`v'],"r") in `v'
> }
> replace ind=1 if (ind>1 & ind !=.)
> ***********************************************
> 
> And this code works fine, though it's a different kind of loop.
> ***********************************
> scalar n=_N
> 
> local i=1
> 	while `i'<n {
> 		di "`i'"
> 	local i=`i'+1
> }
> ***********************************
> 

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