Statalist The Stata Listserver


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

st: Re: eliminate negative values and their positive counterpart


From   "Sergiy Radyakin" <[email protected]>
To   <[email protected]>
Subject   st: Re: eliminate negative values and their positive counterpart
Date   Tue, 6 Mar 2007 20:20:49 +0100

Hello Sara,

you will get a dozen of ways to do this more efficiently in several minutes, but here is a straightforward way:

Assume your data is:
+------------+
| t charge |
|------------|
1. | 1 10 |
2. | 2 20 |
3. | 2 20 |
4. | 3 30 |
5. | 3 20 |
|------------|
6. | 3 -30 |
7. | 3 -30 |
8. | 4 10 |
9. | 4 20 |
10. | 4 30 |
|------------|
11. | 4 40 |
12. | 5 10 |
13. | 5 20 |
14. | 5 -10 |
15. | 5 -10 |
|------------|
16. | 5 10 |
17. | 5 10 |
18. | 5 20 |
19. | 5 10 |
20. | 5 10 |
|------------|
21. | 5 10 |
22. | 5 -10 |
23. | 5 20 |
24. | 5 -20 |
+------------+


Then



gen abs_charge=abs(charge)
sort t abs_charge
by t abs_charge:egen npos=count if charge>0

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

by t abs_charge:egen npos=count(posit)
by t abs_charge:egen nneg=count(negat)

by t abs_charge:gen ndel=npos*(npos<nneg)+nneg*(nneg<npos)

by t abs_charge:drop if _n<ndel | _n>_N-ndel


Or something similar? change t as appropriate to define a group.
regards, sergiy




----- Original Message ----- From: "sara borelli" <[email protected]>
To: <[email protected]>
Sent: Tuesday, March 06, 2007 7:48 PM
Subject: st: eliminate negative values and their positive counterpart



dear satalist members,

This is an exctract of my data:

personid charge proc date
1000124 +13 80048 6/6/2001
1000124 +13 80076 6/6/2001
...
1000124 +13 80048 6/7/2001
1000124 +13 80076 6/7/2001
...
1000124 -13 80048 6/7/2001
1000124 -13 80076 6/7/2001
...
1000124 +13 80048 6/8/2001
...
1000124 +13 80048 6/9/2001
...
1000124 +13 81001 6/11/2001
...
1000124 +13 80048 6/11/2001
1000124 +13 80048 6/12/2001

where the dots indicate that other values for the same
personid are in between. I need to eliminate the
negative charges AND their positive counterpart with
the same proc and date. Thus, for example I need to
eliminate the negative
-13 80048 6/7/2001
AND its positive counterpart with the same proc and
date:
+13 80048 6/7/2001,
and so on.
I should find a way to construct an algorithm that
identifies and eliminates the negatives AND their
poistive counterpart with he same date and procedure,
but I cannot figure that out.
Any help would be really appreciated

thanks
Sara Borelli








___________________________________
L'email della prossima generazione? Puoi averla con la nuova Yahoo! Mail:
http://it.docs.yahoo.com/nowyoucan.html
*
* 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