You pretty well answered your own question.
Stata permits weights in some commands, although not in
-replace-.
Or rather Stata permits subscripts in -replace-,
by virtue of their being part of an expression,
appearing to the RHS of an = sign. In an example
like
. gen d = c
. replace d[1] = 1
weights not allowed
r(101);
Stata's reasoning is, or is equivalent to,
(a) "replace d" I understand.
(b) but what is that "[1]"?
(c) it is to the left of the = sign, so it
is _not_ part of the expression
(d) so the user must intend it as some specification
of weights
(e) but weights are not allowed in -replace-.
There remains the question of what you are trying
to do. I think Alex and Michael misread your question,
as your code is looping over a varlist.
foreach i of varlist x {
replace y=y[`i'-1]+ z[`i']
}
As the varlist contains a single variable, the
loop is redundant here, so this boils down to
replace y = y[x - 1] + z[x]
The implication is that x contains
observation numbers, in effect pointers. Is that right?
It is a way of getting some subtle effects, or you
might be confused.
Nick
n.j.cox@durham.ac.uk
Michael Blasnik
-------------------------------
Actually, the better (faster) way to do this is
replace y=y[`i'-1] +z[`i'] in `i'
-------------------------------
Alex Ogan
-------------------------------
Somebody more knowledgeable will probably explain exactly why you can't
use subscripts on the left side like that.
But I can suggest a workaround:
replace y=y[`i'-1] +z[`i'] if _n==`i'
------------------------------
Shihe Fan
------------------------------
Could any one explain to me why it is OK to write the code in the
following way
gen y=0
foreach i of varlist x {
replace y=y[`i'-1]+ z[`i']
}
but not OK in this way
replace y[`i']=y[`i'-1] +z[`i']
the program always treat the [`i'] on the left side as
weights, instead of subscripts
------------------------------
*
* 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/