Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: RE: RE: Weights vs subscripts


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: RE: RE: Weights vs subscripts
Date   Wed, 4 Oct 2006 17:05:34 +0100

replace X = 5 in 5 if y == z 

Nick 
[email protected] 

> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]]On Behalf Of Fan, Shihe
> Sent: 04 October 2006 16:41
> To: [email protected]
> Subject: st: RE: RE: Weights vs subscripts
> 
> 
> Thanks for your and others' reply. Being a new user, it is always good
> to learn the subtle details of the program. Now suppose that I have a
> vector variable, and want to replace the value of some 
> elements based on
> certain conditions, how should the command be given. Say:
> 
> X[5] = 5 if y == z
> 
> Thank you
> 
> Shihe
>   
> 
> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]] On Behalf Of Nick Cox
> Sent: Wednesday, October 04, 2006 3:35 AM
> To: [email protected]
> Subject: st: RE: Weights vs subscripts
> 
> 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
> [email protected] 
> 
> 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/
> 
> *
> *   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/
> 

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