Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Nick Cox <njcoxstata@gmail.com> |
To | statalist@hsphsun2.harvard.edu |
Subject | Re: st: using loop for [_n+1] |
Date | Thu, 15 Nov 2012 12:02:59 +0000 |
It would be easier to follow your problem if you gave a very simple worked example. Stata follows the existing sort order when using -generate- or -replace-. One consequence is that is usually much easier to work with comparisons with the previous value, not the next value. If you really want a comparison with the next value, reversing the sort order first usually makes that easier. See e.g. SJ-4-4 dm0008 Stata tip 13: generate and replace use the current sort order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . R. Newson Q4/04 SJ 4(4):484--485 (no commands) tips for using generate and replace, which use the current sort order http://www.stata-journal.com/article.html?article=dm0008 FAQ . . . . . . . . . . . . . . . . . . . . . . . Replacing missing values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox 1/12 How can I replace missing values with previous or following nonmissing values or within sequences? http://www.stata.com/support/faqs/data-management/ replacing-missing-values/ Your last problem is easier to diagnose. Second time round the loop, -num- already exists, so -generate- won't play. Usually it is easier to -generate- outside the loop and -replace- inside. gen num = . foreach x of varlist v1-v10 { ... replace num = ... } Nick On Thu, Nov 15, 2012 at 11:21 AM, Dherani, Mukesh <M.K.Dherani@liverpool.ac.uk> wrote: > 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 = > * * 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/