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]

Re: st: keep first two values in each rank


From   Steve Samuels <[email protected]>
To   [email protected]
Subject   Re: st: keep first two values in each rank
Date   Sun, 13 Oct 2013 02:26:35 -0400

.bys rank (value): keep if _n<=2


Steve
[email protected]


On Oct 13, 2013, at 1:31 AM, Evan Lee wrote:

Hello everyone,

I have a dataset like:

date                  value     rank
1990/01/01        a         1
1990/01/01        b         1
1990/01/01        c         1
1990/01/01        d         1
1990/01/01        e         2
1990/01/01         f         2
1990/01/01        g         2
1990/02/01        h         1
1990/02/01         i         1
1990/02/01         j          2
1990/02/01         k         2
1990/03/01          l         1  ......


I would like to keep the first two values for each rank, i.e. keep a &
b, and e & f. I have tried "bysort date (rank): egen rank1 =
rank(rank), unique", but this only gives me:

date                  value     rank   rank1
1990/01/01        a         1          1
1990/01/01        b         1          2
1990/01/01        c         1          3
1990/01/01        d         1         4
1990/01/01        e         2         5
1990/01/01         f         2         6
1990/01/01        g         2         7
1990/02/01        h         1          1
1990/02/01         i         1          2
1990/02/01         j          2         3
1990/02/01         k         2         4
1990/03/01          l         1         1 ......

Have been stuck for quite a while. Could anyone please give me some
help? Much appreciated!!

Kind regards,
Evan
*
*   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/

*
*   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