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

st: RE: newbie- syntax question


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: newbie- syntax question
Date   Tue, 26 Aug 2003 17:58:36 +0100

Rio, Martin
 
> I am writing a code on stata 8 intecooled (win2k), and am 
> running into what
> I guess is a syntax problem.
> As soon as the code gets to the loop below, it returns :
> *yb1 invalid name
> r(198);
> 
> Te loop is:
> 
> while `i' <= `timeunits' {
> 	scalar factor = 10^(`timeunits' - `i')
> 	replace string = string+(`factor'*y`i')
> 	local i = `i' + 1
> }
> 
> I think the syntax error is in the way I try to multiply 
> each variable (y1,
> y2, ..., y`timeunits') by the corresponding factor. The way 
> I am setting it
> up seems consistent with other Stata commands that I have 
> used, but I am no
> an expert on Stata syntax. I'd appreciate if someone could 
> point out my error?

`factor' is, it seems, a local macro which you 
never defined. So Stata sees 

replace string = string + (*y1) 

which is illegal. 

The scalar -factor- and any local macro `factor' 
are completely unrelated. 

You may have meant something like 

tempname factor 
... 
while `i' <= `timeunits' {
 	scalar `factor' = 10^(`timeunits' - `i')
 	replace string = string+(`factor'*y`i')
 	local i = `i' + 1
}

except that the more modern way to do it is 

forval i = 1/`timeunits' { 
	scalar `factor' = 10^(`timeunits' - `i')) 
	replace string = string + (`factor' * y`i')
} 

where I've guessed that the loop starts at 1. 

By the way, is your -string- a _numeric_ variable? 

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