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, statalist.org is already up and running.


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

st: using loop for [_n+1]


From   "Dherani, Mukesh" <M.K.Dherani@liverpool.ac.uk>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   st: using loop for [_n+1]
Date   Thu, 15 Nov 2012 10:37:31 +0000

Dear satalist,
I have few variables both mix of str and numeric entering multiple times. These include company, money, account, date etc.  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" - if I converted all variables to numeric & "type mismatch" if variables are mix of str and numeric.)


foreach x of varlist v1 v2 v3 .....v10 {
sort v1 v2,stable
gen money=mone+mone[_n+1] if `x'=`x'[_n+1]      // here I want to say if the two rows are exactly the same then add money in first row
replace money=mone if `x'!=`x'[_n+1]                      // here I want to say if the two rows are different then replace it with original value
}

I have used  "`x'=`x'[_n+1]" to keep stata happy if there were string variables.  
thank you in advance.

best,
Mukesh 
*
*   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/


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