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
________________________________________
From: owner-statalist@hsphsun2.harvard.edu [owner-statalist@hsphsun2.harvard.edu] on behalf of Maarten Buis [maartenlbuis@gmail.com]
Sent: 15 November 2012 10:50
To: statalist@hsphsun2.harvard.edu
Subject: Re: st: using loop for [_n+1]
On Thu, Nov 15, 2012 at 11:37 AM, Dherani, Mukeshwrote:
> I want to add money if the next row is exactly the same, however stata is coming up with errors when I used following syntax:
> ("invalid syntax"
<snip>
> gen money=mone+mone[_n+1] if `x'=`x'[_n+1]
The if condition should be: -if `x' == `x'[_n+1]-
notice the == instead of =
Hope this helps,
Maarten
---------------------------------
Maarten L. Buis
WZB
Reichpietschufer 50
10785 Berlin
Germany
http://www.maartenbuis.nl
---------------------------------
*
* 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/
*
* 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/