Announcement

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

  • #16
    Thanks. I think I get it now. Here is a demonstration using rangerun from SSC.

    There are lots of details, some simple, some subtle.

    Your variable is evidently not invest but data. Or -- if it is something else -- that is your call.

    Your time window appears to be -30 0, so the previous 30 days and this day. But you need a proper Stata daily date variable. Your string date variable is no use for this purpose.

    There are several definitions of rank and percentile rank. See also https://www.stata.com/support/faqs/s...ing-positions/ for one perspective.

    You may not have an analogue of the panel variable company. If not, there is no by() option call.

    Code:
    webuse grunfeld, clear
    
    capture program drop pcrank 
    
    program pcrank 
        count if invest < invest[_N] & invest < . 
        scalar i1 = r(N)
        count if invest == invest[_N] & invest < . 
        scalar i2 = r(N)
        count if invest < . 
        gen n = r(N)
        gen rank = i1 + i2 
        gen pcrank = (i1 + 0.5 * i2) / n 
    end 
    
    rangerun pcrank, use(invest) int(year -9 0) by(company)
    
    list invest n rank pcrank if company == 1 
    
         +-------------------------------+
         | invest    n   rank     pcrank |
         |-------------------------------|
      1. |  317.6    1      1         .5 |
      2. |  391.8    2      2        .75 |
      3. |  410.6    3      3   .8333333 |
      4. |  257.7    4      1       .125 |
      5. |  330.8    5      3         .5 |
         |-------------------------------|
      6. |  461.2    6      6   .9166667 |
      7. |    512    7      7   .9285714 |
      8. |    448    8      6      .6875 |
      9. |  499.6    9      8   .8333333 |
     10. |  547.5   10     10        .95 |
         |-------------------------------|
     11. |  561.2   10     10        .95 |
     12. |  688.1   10     10        .95 |
     13. |  568.9   10      9        .85 |
     14. |  529.2   10      6        .55 |
     15. |  555.1   10      7        .65 |
         |-------------------------------|
     16. |  642.9   10      9        .85 |
     17. |  755.9   10     10        .95 |
     18. |  891.2   10     10        .95 |
     19. | 1304.4   10     10        .95 |
     20. | 1486.7   10     10        .95 |
         +-------------------------------+

    Comment


    • #17
      Thanks a lot for your continuous help Nick.

      I have used the following code which seems to work, but all the observations get the same value (0.71xx).
      Total_buys is the variable I want to rank. id is a unique identifier for each observation. Date is a date which I believe is in good Stata format. Am I making any mistake? Thanks again!

      Code:
      program pcrank 
          count if total_buys < total_buys[_N] & total_buys < . 
          scalar i1 = r(N)
          count if total_buys == total_buys[_N] & total_buys < . 
          scalar i2 = r(N)
          count if total_buys < . 
          gen n = r(N)
          gen rank = i1 + i2 
          gen pcrank = (i1 + 0.5 * i2) / n 
      
      rangerun pcrank, use(total_buys) int(Date -30 -1) by(id)

      Comment


      • #18
        It is likely that you don't need by(id), which enforces ranking within id. As said in #16

        You may not have an analogue of the panel variable company. If not, there is no by() option call.

        Comment

        Working...
        X