Announcement

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

  • Trying to rank numbers without gaps

    Hello,

    I'm using Stata 14.2, and I'm trying to figure out how to rank a set of numbers using sequential integers, rather than leaving gaps in the case of ties. For example, I have data in the format of the first column, and I would like to generate a new variable shown in the second column:

    Current_rank New_rank_no_gaps
    1 1
    2 2
    3 3
    3 3
    5 4
    6 5
    6 5
    8 6
    9 7
    10 8
















    As far as I know, the egen command doesn't have this option; the track option for rank produces the same ranking in the first column. I can accomplish my goal with an array in R by finding the position in an array of unique values (explained further in the comments here) but I'd like to get it done in Stata if possible. Does anyone have any ideas? Thanks in advance!

  • #2
    Stata will produce ranks that are consecutive integers if there are no ties in the data. If there are ties, then no unique ranking is possible. Apparently your data have ties. So, if you are not bothered by random breaking of ties, you could do
    Code:
    sort YourVariable
    gen int rank = _n
    You might prefer sort, stable. See -help sort-.

    Comment


    • #3
      To me, this isn't ranking, it's grouping. Here are two ways to do it.

      Code:
      clear
      input Current_rank    New_rank_no_gaps
      1    1
      2    2
      3    3
      3    3
      5    4
      6    5
      6    5
      8    6
      9    7
      10    8
      end 
      
      egen sol1 = group(Current_rank), label 
      
      gen sol2 = sum(Current != Current[_n-1]) 
      * labmask is from Stata Journal  
      labmask sol2, values(Current) 
      
      . list, sepby(Current) 
      
           +-----------------------------------+
           | Curren~k   New_ra~s   sol1   sol2 |
           |-----------------------------------|
        1. |        1          1      1      1 |
           |-----------------------------------|
        2. |        2          2      2      2 |
           |-----------------------------------|
        3. |        3          3      3      3 |
        4. |        3          3      3      3 |
           |-----------------------------------|
        5. |        5          4      5      5 |
           |-----------------------------------|
        6. |        6          5      6      6 |
        7. |        6          5      6      6 |
           |-----------------------------------|
        8. |        8          6      8      8 |
           |-----------------------------------|
        9. |        9          7      9      9 |
           |-----------------------------------|
       10. |       10          8     10     10 |
           +-----------------------------------+
      
      . 
      . list, sepby(Current) nola 
      
           +-----------------------------------+
           | Curren~k   New_ra~s   sol1   sol2 |
           |-----------------------------------|
        1. |        1          1      1      1 |
           |-----------------------------------|
        2. |        2          2      2      2 |
           |-----------------------------------|
        3. |        3          3      3      3 |
        4. |        3          3      3      3 |
           |-----------------------------------|
        5. |        5          4      4      4 |
           |-----------------------------------|
        6. |        6          5      5      5 |
        7. |        6          5      5      5 |
           |-----------------------------------|
        8. |        8          6      6      6 |
           |-----------------------------------|
        9. |        9          7      7      7 |
           |-----------------------------------|
       10. |       10          8      8      8 |
           +-----------------------------------+

      Comment


      • #4
        Naturally, don't ask for value labels if you don't want them.

        Comment


        • #5
          Nick, thank you for pointing out the group function! This does almost exactly what I need, except for one thing I neglected to mention: my dataset contains several groups already, and I would like to produce a "ranking" within each group.

          Luckily, I found a stack overflow thread where you already answered this exact question!

          Here is a more complete example showing the resolution with sol2:

          Code:
          clear
          input str6 Proposal_group Current_rank New_rank_no_gaps
          "A" 1 1
          "A" 2 2
          "A" 3 3
          "A" 3 3
          "A" 5 4
          "A" 6 5
          "A" 6 5
          "A" 8 6
          "A" 9 7
          "A" 10 8
          "B" 1 1
          "B" 2 2  
          "B" 2 2
          "B" 4 3
          "B" 5 4
          end
          egen sol1 = group(Proposal_group Current_rank)
          bysort Proposal_group (Current_rank): gen sol2 = sum(Current_rank != Current_rank[_n-1]) if Current_rank != .
          
          list, sepby(Current)
          
             
               +----------------------------------------------+
               | Propos~p   Curren~k   New_ra~s   sol1   sol2 |
               |----------------------------------------------|
            1. |        A          1          1      1      1 |
               |----------------------------------------------|
            2. |        A          2          2      2      2 |
               |----------------------------------------------|
            3. |        A          3          3      3      3 |
            4. |        A          3          3      3      3 |
               |----------------------------------------------|
            5. |        A          5          4      4      4 |
               |----------------------------------------------|
            6. |        A          6          5      5      5 |
            7. |        A          6          5      5      5 |
               |----------------------------------------------|
            8. |        A          8          6      6      6 |
               |----------------------------------------------|
            9. |        A          9          7      7      7 |
               |----------------------------------------------|
           10. |        A         10          8      8      8 |
               |----------------------------------------------|
           11. |        B          1          1      9      1 |
               |----------------------------------------------|
           12. |        B          2          2     10      2 |
           13. |        B          2          2     10      2 |
               |----------------------------------------------|
           14. |        B          4          3     11      3 |
               |----------------------------------------------|
           15. |        B          5          4     12      4 |
               +----------------------------------------------+

          Comment

          Working...
          X