Statalist


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

RE: st: using _all


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: using _all
Date   Thu, 28 Aug 2008 13:06:32 +0100

Eva is correct, as usual. Some further notes for Ashim and anyone
interested: 

The syntaxes -foreach ... in ...- and -foreach ... of ...- are very
different. 

With the first -foreach- takes what you typed and treats it very
literally. There is no translation, expansion, or substitution. 

With the second -foreach- is inclined to expand whatever is specified
afterwards, e.g. a varlist specified as such will be expanded to the
constituent variable names. 

With this code 

. foreach x in _all {
   replace  `x'=`x'[_n-1] if _n==_N
}

You have specified a list with a single item, namely _all, and -foreach-
pays no attention to what the item means. It is entirely as if you have
specified 

replace _all = _all[_n-1] if _n == _N 

which happens to be illegal. So, the -foreach- specification is legal,
but what it implies within the loop is not. To drive that home, consider
this 

foreach x in _all { 
	d `x' 
} 

The example is dopey, but it illustrates that you could have a perfectly
legal example. 

(Why is it dopey? 

Because there is little point in setting up a loop with a single item. 

Because -d _all- is just more easily written -d-.) 

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

P.S. On a different note, what you are doing seems a bit odd, but there
is another way to do it: 

drop in L 
expand 2 in L 

Eva Poen

you want to change the first line:

foreach x of varlist _all {

Ashim Kapoor

> I wish to replace the last obs of EACH variable by the previous value.
> But when I do this : -
> . foreach x in _all {
>   replace  `x'=`x'[_n-1] if _n==_N
>  }
>
> it does'nt work.!

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   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