You can't -generate- a variable that already exists.
That is basic: read about -generate- and -replace-.
You may want to -generate- outside the loop
and -replace ... in `i'- within the loop.
gen X = .
forval i = 44/93 {
replace ... in `i'
}
Asgar Khademvatani
Basically, I am estimating a cost model using time series dataset over
1958-2000. Then, I have appended a new observation over 2001-2050 to my
master dataset in the Stata memory, using--append-- command. I want to
generate some new variables defined based on functions upon estimated
coefficient and the other variables, I want generate these variables
only over 2001-2050 during the same period of new using
observation.Actually, I already tried --gen-- command but it only
generate the variables over master dataset period over 1958-2000 NOT
over 2001-2050. In doing so, I am going to write a --forvalues-- loop to
do so. But the variables only are generated at one single
observation,say only 2001, and it is re-extended over other years with
the same values . For example, my written loop and the stata results are
given as:
A Sample Loop:
forvalues i = 44/93 {
gen X = _b[le_sg] + _b[lple_sg]*(plpl_sg[`i']) +
_b[lpme_sg]*(plpm_sg[`i']) + _b[lek_neg_sg]*(plk_sg[`i']) +
_b[let_sg]*(pt[`i'])
list X
}
The Stata Output:
do "C:\DOCUME~1\ASGARK~1\LOCALS~1\Temp\STD0i000000.tmp"
. 1. forvalues i = 44/93 {
2. gen X =_b[le_sg] + _b[lple_sg]*(plpl_sg[`i']) +
_b[lpme_sg]*(plpm_sg[`i']) + _b[lek_neg_sg]*(plk > _sg[`i']) +
_b[let_sg]*(pt[`i'])
3. list X
4. }
44
+-----------+
| X
|-----------|
1. | -7.730469 |
94. | -7.730469 |
+-----------+
X already defined
r(110);
end of do-file
As you see in the above, X variable is generated only in 2001 and it is
repeated over next years with the same value. I am not sure what is
wrong with my loop. May somebody please help me out on this matter and
tell me what is wrong with that? Otherwise, I am just wondering that is
there another way other than writing a loop for generating the variables
over 2001-2050?
*
* 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/