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

st: RE: while vs. forvalues


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: while vs. forvalues
Date   Sat, 7 Aug 2004 16:12:55 +0100

I agree: the two code fragments should
be equivalent. (Stata indulges `j ' for `j'.) 

I put this code in my do-file editor and 
it ran without problem, so I suspect 
some other problem. 

tempname newvec rowvec 
matrix `rowvec' = J(10,1,1) 
matrix `newvec' = J(10,1,2) 
local n = 10 

mat li `newvec' 

forvalues j = 1(1)`n' {
	mat `newvec'[`n'-`j'+1,1] = `rowvec'[`j',1]
}

mat li `newvec' 

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

Michael S. Hanson
 
> 	I tried the following code snippet in a program, but I 
> kept getting an 
> "invalid syntax" error at the -forvalues- command (according to 
> -trace-):
> 
> 	forvalues j = 1(1)`n' {
> 		mat `newvec'[`n'-`j'+1,1] = `rowvec'[`j',1]
> 	}
> 
> When I replaced that code with the following, the program 
> completed as 
> expected:
> 
> 	local j = 1
> 	while `j '<= `n' {
> 		mat `newvec'[`n'-`j'+1,1] = `rowvec'[`j',1]
> 		local ++j
> 	}
> 
> I'm puzzled by this, as my reading of p.214 of the User's Guide 
> suggests these two approaches should be equivalent.  Any ideas?

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