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 on April 23, and its replacement, statalist.org is already up and running.


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

Re: st: Creating ten 10%-portfolios


From   Maarten Buis <maartenlbuis@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Creating ten 10%-portfolios
Date   Sat, 2 Mar 2013 14:42:27 +0100

There is no need. Bo misunderstood Nick by looking at the -xtile-
command instead of the -xtile- function in -egenmore- (SSC).

-- Maarten

On Sat, Mar 2, 2013 at 12:13 AM, Yu Chen, PhD <profyuchen@gmail.com> wrote:
> If Stata Corp. can improve -xtile- by enabling by: prefix, that will
> be much better.
>
>
>
> On Fri, Mar 1, 2013 at 2:11 PM, Bo Spiljard <bo.spiljard@gmail.com> wrote:
>> 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/
> *
> *   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/



-- 
---------------------------------
Maarten L. Buis
WZB
Reichpietschufer 50
10785 Berlin
Germany

http://www.maartenbuis.nl
---------------------------------
*
*   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