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

RE: st: using _all

From   "Nick Cox" <>
To   <>
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

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

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

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

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-.) 


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:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index