Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

# Re: st: Creating ten 10%-portfolios

 From Bo Spiljard To statalist@hsphsun2.harvard.edu Subject Re: st: Creating ten 10%-portfolios Date Fri, 1 Mar 2013 21:11:47 +0100

```Thank you for your solution!
I was not informed that the -xtile()- function could do this.
After a couple tries and some fiddling I managed to do it with this function.

It is not the most tidy solution it does the work.

The code I used:

gen illiq2=illiq

sort id year month illiq2

quietly by id year month illiq2: gen dup=cond(_N==1,0,_n)

replace illiq2=. if dup>1

xtile temp = illiq2, nq(10)

by id year month: egen portfolio1=max(temp)

drop dup

drop illiq2

drop temp

sort id year month day

On Fri, Mar 1, 2013 at 6:39 PM, Nick Cox <njcoxstata@gmail.com> wrote:
> It sounds as if you need to check out the -xtile()- function in
> -egenmore- (SSC).
>
> Alternatively, here is a solution from first principles for what I
> think you are asking, but I have just written this down, not tested
> it.
>
> gen mdate = ym(year month)
> egen group = group(mdate)
> su group, meanonly
> local max = r(max)
>
> gen portfolio = .
>
> qui forval i  = 1/`max' {
>         xtile temp=illiq if group == `i'
>         replace portfolio = temp if group == `i'
>         drop temp
> }
>
>
> Nick
>
> On Fri, Mar 1, 2013 at 4:00 PM, Bo Spiljard <bo.spiljard@gmail.com> wrote:
>
>  I am testing a version of the
>> CAPM and I need to make ten 10-percentile portfolios (sub groups) and
>> these have to be revised each month. This has to be done by creating a
>> new variable that contains the numbers 1 to 10 and each number should
>> represent a portfolio.
>>
>> Normally if I am stuck I could find the answer by searching on
>> statalist, but this problem I could not find. I allready tried alot of
>> things myself (i.e. using r(p10)) etc, but I cannot find the right
>> code. The data I have is: id, date (year, month and day), and illiq. I
>> need to create 10 equal portfolios based on the proportion of illiq
>> and this have to be revised each month. So each of the portfolio at
>> each month has to contain the same amount of ids, with in portfolio 1
>> the id with the lowest value of illiq, in portfolio 2 the id with
>> slightly higher illiq, etc..
>>
>> Also the data for each firm does not start and not end at the same
>> date and also for each date there is missing information, so I cannot
>> purely use the sort function.
>>
>> Here is an simplified example with allready portfolios added how it
>> should look like.
>>
>> id            year       month  day        illiq         portfolio
>>
>> 1             2001      1             1             15           3
>>
>> 2             2001      1             1             13           2
>>
>> 3             2001      1             1             12           1
>>
>> 1             2001      1             2             11           3
>>
>> 2             2001      1             2             8             2
>>
>> 3             2001      1             2             13           1
>>
>> ...
>>
>> 1             2001      2             1             10           3
>>
>> 2             2001      2             1             9             2
>>
>> 3             2001      2             1             7             1
>>
>> ...
>>
>> 1             2009      12           31           8             1
>>
>> 2             2009      12           31           11           2
>>
>> 3             2009      12           31           13           3
>>
>>
> *
> *   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/
```