Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

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

 From "Sarah Edgington" <[email protected]> To <[email protected]> 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
statement.

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

-Sarah

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Dherani, Mukesh
Sent: Thursday, November 15, 2012 3:22 AM
To: [email protected]
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.
m

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/
```