Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | George Murray <george.murray16@gmail.com> |
To | statalist@hsphsun2.harvard.edu |
Subject | Re: st: Working with row percentiles and row averages |
Date | Tue, 9 Jul 2013 23:39:58 +1000 |
Nick, once again, thank you very very much for this, it saved me a lot of time. I've read through the relevant section of the pdf and will be happy to cite it once my article is finished. I was actually playing around with the forval command, but didn't think of this. On Tue, Jul 9, 2013 at 11:27 PM, Nick Cox <njcoxstata@gmail.com> wrote: > Let's imagine that we have -pc10- and we want the average of all > returns less than that. The average is naturally the sum divided by > the count. So, we go > > gen count = 0 > gen sum = 0 > > quietly forval j = 1/1000 { > replace count = count + 1 if r`j' < pc10 > replace sum = sum + r`j' if r`j' < pc10 > } > > gen ave10 = sum/count > > > There is a review at > > SJ-9-1 pr0046 . . . . . . . . . . . . . . . . . . . Speaking Stata: Rowwise > (help rowsort, rowranks if installed) . . . . . . . . . . . N. J. Cox > Q1/09 SJ 9(1):137--157 > shows how to exploit functions, egen functions, and Mata > for working rowwise; rowsort and rowranks are introduced > > A .pdf is accessible at > > http://www.stata-journal.com/sjpdf.html?articlenum=pr0046 > > If it's valuable, please cite it. > Nick > njcoxstata@gmail.com > > > On 9 July 2013 14:15, George Murray <george.murray16@gmail.com> wrote: >> abcde >> Dear Statalist, >> >> I am working with time series data, and my variables consist of >> monthly returns for 1000 stocks (so the variables are >> date,r1,r2,r3,...,r1000). I would like to find, for each date, the >> average return of the stocks whose return is below that of the 10th >> percentile. That is, for the 10% of stocks with the lowest returns, I >> want to compute their average, for each date. >> >> I am aware that a command such as -egen p10 = rowpctile(r1-r1000), >> p(10)- generates a variable which computes the return of the 10th >> percentile at each date, but I don’t know how to go from here to find >> the average return below the 10th percentile, for each date. >> >> Any ideas for commands would be very highly appreciated. This is the >> first time I have had to work ‘by rows’, and am very stuck with this. >> >> Thank you. >> >> * >> * 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/