Bookmark and Share

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


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

st: using loop for [_n+1]


From   "Dherani, Mukesh" <[email protected]>
To   "[email protected]" <[email protected]>
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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index