# Re: st: Decile sorts

 From Philipp Rehm To statalist@hsphsun2.harvard.edu Subject Re: st: Decile sorts Date Thu, 09 Nov 2006 23:20:35 +0100

On 1)
You may find the user-written program -sumdist- useful (type "findit sumdist") to, well, find it.

HTH,
Ph

Thomas Erdmann wrote:

Hi,

I am trying to sort my observations into deciles according to one attribute
and afterwards calculating the average of another attribute of those ten
groups.
(Basically different kinds of financial ratios for a group of companies to
check afterwards, if the decile portfolios have significantly different
rates of return).
Please find the code I came up with below [lines with ... are omitted], yrm
is the time variable (YearMonth)

(1) As far as I can tell it works out, but a) it's a lot of code and
b)produces a lot of variables and c)generating the output is rather awkward.

Could you give me hints on how to implement a smarter solution or if there
are any errors in the way the calculation is carried out currently?

(2) In an earlier mail it was suggested to use -anova- to assess the
equality of portfolios. I now found a study which uses a Chi-Square
statistic to assess the null hypothesis of equality of portfolios by using
GMM with the moment conditions: e1= R1 - MR; e2= R2 - MR; ...; e10 = R10 - MR
where R1 to R10 are the returns of the decile portfolios and MR is the Mean
Return parameter to be estimated (therefore over-identified equation with 10
moment conditions and only one parameter to be estimated). How can this be
translated into Stata commands?

Thanks a lot.
Tom

*------------------------------
* DECILE PORTFOLIOS
*------------------------------

*** Generate Percentiles
sort yrm
foreach X of varlist c1* {
by yrm: egen p10_`X'= pctile(`X'), p(10.0)
by yrm: egen p20_`X'= pctile(`X'), p(20.0)
by yrm: egen p30_`X'= pctile(`X'), p(30.0)
...
by yrm: egen p90_`X'= pctile(`X'), p(90.0)
}

*** Sort into Percentile groups
foreach X of varlist c1* {
gen G_`X'=1 if `X'<p10_`X' & `X'~=.
replace G_`X'=2 if `X'>p10_`X' & `X'<p20_`X' ... replace G_`X'=9 if `X'>p80_`X' & `X'<p90_`X' replace G_`X'=10 if `X'>p90_`X' & `X'~=.
}

*** Calculate return mean for each group
sort yrm
foreach X of varlist G* {
by yrm: egen R1`X'= mean(c1ds_ri) if `X'==1
by yrm: egen R2`X'= mean(c1ds_ri) if `X'==2
...
by yrm: egen R9`X'= mean(c1ds_ri) if `X'==9
by yrm: egen R10`X'= mean(c1ds_ri) if `X'==10
}

*** Results
preserve
collapse (mean) R*
summarize
restore

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