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

st: RE: RE: RE: local variable and indexing into a variable


From   "David E Moore" <davem@hartman-group.com>
To   statalist@hsphsun2.harvard.edu
Subject   st: RE: RE: RE: local variable and indexing into a variable
Date   Fri, 24 Jun 2005 09:12:07 -0700

Nick is, of course, absolutely correct.  (NB, the last character is the
lowercase el, not the number 1.)  Moreover, he makes another point, albeit
indirectly.  That is, Stata often can get you to the result you want by more
than one path, but one may take advantage of Stata features that offer
significant efficiency gains.  The moral of the story is it pays to think in
Stata-ese.


-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu]On Behalf Of Nick Cox
Sent: Friday, June 24, 2005 12:59 AM
To: statalist@hsphsun2.harvard.edu
Subject: st: RE: RE: local variable and indexing into a variable


What David suggests is fine and -- more to the point --
legal -- whereas what Florence tried is illegal. The
way to see this is to look at the syntax diagram
for -replace-, which makes it clear that a existing
varname (only) can appear on the LHS of an equals
sign.

A quite different point is that David's suggestion
obliges Stata to test every observation to see
if _n == _N.

replace `var' = 0 in l

lets Stata go straight to the last observation.

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

David E Moore

> Stata prefers:
>
> replace `var' = 0 if _n==_N

NEYMOTIN, FLORENCE

> Hi, I tried typing:
>
> foreach var in x y {
> replace `var'[_N]=0
> }
>
> then it considers [_N] a "weight" when clearly I want it to
> just change the last
> observation of
> both x and y to be zeroes.
>
> alternatively, I tried the inner line as: ( replace
> `var[_N]'=0 )    and then it
> doesn't recognize
> var[_N] and thinks that it is an entirely new local reference.
> Is there a correct syntax here, or does it look like there is
> some other sort of mistake in my coding ?

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index