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 at the end of May, and its replacement, statalist.org is already up and running.


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

Re: st: Keep observations in 1/100 by country and year


From   Ryan Stevens <stevrl7@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Keep observations in 1/100 by country and year
Date   Tue, 7 May 2013 11:14:28 -0400

The gsort command is correct, but you are resorting your dataset again
when using bysort. Instead a simple by command would work. Using
Stata's built in observation macro, _n, should do the trick:

gsort country year -tot_sales
gen top100 = 0
by country year: replace top100 = 1 if _n <= 100

Ryan

On Tue, May 7, 2013 at 10:49 AM, Estrella Gomez <estrellastata@gmail.com> wrote:
>
> Hi, statalisters
>
> I have a dataset with songs by year and country. I would like to keep
> only the top 100 songs for each country each year (there are 18
> countries and 6 years). I have tried the following:
>
> gsort country year -tot_sales
> *because I want the songs with higher number of sales
>
> gen top100=0
> bysort country year: replace top100=1 in 1/100
> *to generate a dummy that takes value one for the top 100 songs, so I
> can drop observations that equals zero
>
> However, Stata says that it's not possible to combine "by" with "in"
>
> Any idea?
> Thanks a lot,
> Estrella
> *
> *   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index