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 observations in 1/100 by country and year


From   Estrella Gomez <[email protected]>
To   [email protected]
Subject   Re: st: Keep observations in 1/100 by country and year
Date   Tue, 7 May 2013 17:24:09 +0200

Thanks!

2013/5/7 Ryan Stevens <[email protected]>:
> 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 <[email protected]> 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/
*
*   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