Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, is already up and running.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: st: using loop for [_n+1]

From   "Sarah Edgington" <>
To   <>
Subject   RE: st: using loop for [_n+1]
Date   Thu, 15 Nov 2012 08:32:20 -0800

I don't think you need a loop for what you want to do.
It seems like what you want is:
gen money=mone+mone[_n+1] if v1==v1[_n+1] & v2==v2[_n+1] & v3==v3[_n+1] ...
& v10==v10[_n+1] 
(where the ellipsis represents the equalities for v4-v9 that I am too lazy
to write out at the moment)
You don't need a loop for that.  You just need to write out the whole if

As an aside, even changing the = to == should not have fixed your original
example because you still have a -gen- command that repeats each time the
loop is iterated.  Stata can't generate a new variable if it already exists
(which in your example it would since money is generated inside the loop).


-----Original Message-----
[] On Behalf Of Dherani, Mukesh
Sent: Thursday, November 15, 2012 3:22 AM
Subject: RE: st: using loop for [_n+1]

Thanks Martin, it does run without any complaints.  However, it does not
provide the required.  It compares each variable of next row as 'or' rather
than 'and'. I wanted to compare two rows and if they are exactly the same
then it should add money in the preceding row. Using this syntax it is
adding money even only one variable is equal in value in the next row. 

Secondly, I wanted to assess which two rows are exactly the same using
following command:

foreach x of varlist v1-v10 {
sort v1 v2,stable
gen num=1 if `x'==`x'[_n+1] | `x'==`x'[_n-1] }

this keep running the loop, error : num already defined.

thanks again. 

*   For searches and help try:

© Copyright 1996–2016 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index