Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Panel data, generate a variable if it belongs to the top quantile per date

    Dear Statalisters,

    First of all I want to state that I am aware that is common practice to use both your first and last name on this forum. Most common argument given was that users with bad behaviour can hide behind their username. However, I show nothing more than respect to you as I come to you to seek help with my problem. I hope that you will respect my choice to hide my last name as I am very cautious nowadays where on the internet my full name pops up.

    Below you will find a simplified example of my panel data set which contains the stock returns of different stocks represented by id.
    I'd like to generate a variable that contains a 1 if the return of that stock belonged to the 40 percent highest observations of that day.

    I have used the xtset function and for now I have this part of the code. However, this does not assess the top 40 daily. It assesses if it is in the top 40 of all observation considered on all days. I'd like to use the function by(date), however this is not possible for the xtile function.

    Code:
    xtile Q = return,n(10)
    gen top40 = 1 if inlist(Q,7,8,9,10)

    Thank you for your time in advance,


    Regards,

    Emilio


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(id date return)
    1 20454   .3488717
    1 20455   .2668857
    1 20456   .1366463
    1 20457 .028556867
    1 20458   .8689333
    2 20454   .3508549
    2 20455  .07110509
    2 20456  .32336795
    2 20457   .5551032
    2 20458    .875991
    3 20454  .20470947
    3 20455   .8927587
    3 20456   .5844658
    3 20457   .3697791
    3 20458   .8506309
    4 20454   .3913819
    4 20455  .11966132
    4 20456   .7542434
    4 20457   .6950234
    4 20458   .6866152
    end
    format %td date

  • #2
    The simplest solution is to install the -egenmore- package from SSC (-ssc install egenmore-). It contains an -egen, xtile()- function that can be used with -by:-.

    Comment


    • #3
      Dear Clyde,

      Thanks for the advice, I have used the following line:
      Code:
      egen Q = xtile(return), by (date) n(10)
      This should do the trick, right?

      Thanks a lot!

      Comment


      • #4
        Yes, that looks right.

        Comment

        Working...
        X