Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: eliminate oppositie positive and negative values


From   "Sergiy Radyakin" <[email protected]>
To   <[email protected]>
Subject   Re: st: eliminate oppositie positive and negative values
Date   Thu, 29 Mar 2007 14:49:44 +0200

clear
input num personid date charge proc xm
406 124 2001 -5274 121 -1050
409 124 2001 -5274 121 -1050
409 124 2001 -5274 121 -1050
408 124 2001 5274 121 1050
451 124 2001 5274 121 1050
453 124 2001 5274 121 1768
end

l

gen whatever_id=string(personid)+"#"+string(date)+"#"+string(proc)+"#"+string(abs(xm))
gen abs_charge=abs(charge)

gen posit=1 if charge>0
gen negat=1 if charge<0

sort whatever_id abs_charge charge
by whatever_id abs_charge:egen npos=count(posit)
by whatever_id abs_charge:egen nneg=count(negat)

by whatever_id abs_charge:gen ndel=npos*(npos<=nneg)+nneg*(nneg<npos)
gen flag=0
by whatever_id abs_charge:replace flag=1 if _n<=ndel | _n>_N-ndel
drop if flag
drop abs_charge posit negat npos nneg ndel flag whatever_id
l




Sergiy



----- Original Message ----- From: "Michael Blasnik" <[email protected]>
To: <[email protected]>
Sent: Thursday, March 29, 2007 2:42 PM
Subject: Re: st: eliminate oppositie positive and negative values



Woops, I copied your own syntax errors below. Clearly, yoiu can't have a variable named abs(xm) and the code should be changed to absxm. Also, I would combine replace todrop line in with the gen todrop line as one logical condition, since it should be faster that way, especially if you are working with a large dataset and many times though the loop.

Michael

----- Original Message ----- From: "Michael Blasnik" <[email protected]>
To: <[email protected]>
Sent: Thursday, March 29, 2007 8:32 AM
Subject: Re: st: eliminate oppositie positive and negative values



That wouldn't work because there could be offsetting charges and non offsetting charges on the same date so the sum wouldn't be zero.

Although there may be better ways to do this, to repeat the loop until there is nothing to change you can use while loop that continues based on a test.

gen abscharge=abs(charge)
gen abs(xm)=abs(xm)
local go=1
while `go' {
bysort personid proc date abscharge abs(xm)(charge) :
gen byte todrop=charge!=charge[_n-1] & _n>1
bysort personid proc date abscharge absxm (charge) :
replace todrop=1 if charge!=charge[_n+1] & _n<_N
qui count if todrop==1
if r(N)==0 local go=0
drop if todrop==1
drop todrop
}


Michael Blasnik

----- Original Message ----- From: "Ron�n Conroy" <[email protected]>
To: <[email protected]>
Sent: Thursday, March 29, 2007 5:11 AM
Subject: Re: st: eliminate oppositie positive and negative values



On 29 M�rta 2007, at 02:41, sara borelli wrote:

I need to drop the negatives and the positives with
the same date abs(charge) abs(xm) proc personid. As
suggested by Michael Blasnik in a previous e-mail I
runned the following:
doesn't that mean that the charges for a given date should sum to zero?

If that's the case then can't you just sum the charges within each person within each date and check if they are zero?
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index