Announcement

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

  • Ranking of players in a group based on score variable

    Hi Everyone,

    I am working with the following experiment data, where I have provided a sample below. The variables are
    participantcode:
    Unique for each person in a treatment,

    Treatment :
    Each person has only one treatment,

    Round:
    Number of rounds for each person where max = 8 and each person has an observation for each round,

    P2_group_id:
    In each round, three players make a group and this is the group ID. In each round, players are randomly rematched again and new groups are formed,

    P2_player_id:
    In each group, this variable represents the randomly assigned id within the group. Hence, a person may have different P2_player_id in each round because it is round specific id.

    T1_success:
    Number of correct answers. These scores are given and does not change across rounds. However, the data on that is entered only in Round 1 for each unique player (Is it better to auto fill it in the remaining rounds for a player using unique id of the player?).

    sessioncode:
    Unique for each sessions. Two different sessions may have the same treatment but with different people.


    Problem:
    1) I want to create a variable that ranks the players in each group for each round based on their number of correct answers. Specifically, in the case of tie when all three scored the same, I want the rank to be the same as P2_player_id. If the first rank is clear but tie is for the second and third place then second rank will be given to the player with lower P2_player_id among the two. Similarly, if the tie is for the first and second position, the first rank is given to person with lower P2_player_id among the two.

    While doing so, I want to make sure that I do that for all groups. Groups do not have unique id, they have the numbers. Hence, I need to use the "by" condition. I think of it as starting by sorting data first by session_code, then Round, and then P2_group_id


    2) Following from above, I also want to create three dummy variables for each group that reports whether there was any kind of tie in scores (i.e. tie for all three ranks, tie for first and second position, tie for second and third position).


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str8 participantcode byte(P2_group_id P2_player_id Round T1_success Treatment) str8 sessioncode
    "xp5dr3wz" 3 2 1 33 3 "30z0wyr2"
    "zwd8oerg" 4 1 1 45 3 "30z0wyr2"
    "kdrn04zt" 2 3 1 31 3 "30z0wyr2"
    "kz0pah98" 3 3 1 43 3 "30z0wyr2"
    "zvg9zop4" 1 2 1 35 3 "30z0wyr2"
    "uabc3w2i" 1 3 1 31 3 "30z0wyr2"
    "hidkbh57" 4 3 1 36 3 "30z0wyr2"
    "4s8fggab" 3 1 1 39 3 "30z0wyr2"
    "mgb7nktz" 4 2 1 35 3 "30z0wyr2"
    "261sg8w7" 2 2 1 36 3 "30z0wyr2"
    "mo19or6c" 1 1 1 24 3 "30z0wyr2"
    "7unz5s7y" 2 1 1 39 3 "30z0wyr2"
    "xp5dr3wz" 2 3 2  . 3 "30z0wyr2"
    "zwd8oerg" 1 2 2  . 3 "30z0wyr2"
    "kdrn04zt" 3 1 2  . 3 "30z0wyr2"
    "kz0pah98" 1 1 2  . 3 "30z0wyr2"
    "zvg9zop4" 3 3 2  . 3 "30z0wyr2"
    "uabc3w2i" 1 3 2  . 3 "30z0wyr2"
    "hidkbh57" 3 2 2  . 3 "30z0wyr2"
    "4s8fggab" 2 1 2  . 3 "30z0wyr2"
    "mgb7nktz" 4 1 2  . 3 "30z0wyr2"
    "261sg8w7" 4 3 2  . 3 "30z0wyr2"
    "mo19or6c" 4 2 2  . 3 "30z0wyr2"
    "7unz5s7y" 2 2 2  . 3 "30z0wyr2"
    "xp5dr3wz" 3 1 3  . 3 "30z0wyr2"
    "zwd8oerg" 2 3 3  . 3 "30z0wyr2"
    "kdrn04zt" 3 2 3  . 3 "30z0wyr2"
    "kz0pah98" 4 3 3  . 3 "30z0wyr2"
    "zvg9zop4" 4 1 3  . 3 "30z0wyr2"
    "uabc3w2i" 1 1 3  . 3 "30z0wyr2"
    "hidkbh57" 1 2 3  . 3 "30z0wyr2"
    "4s8fggab" 2 1 3  . 3 "30z0wyr2"
    "mgb7nktz" 2 2 3  . 3 "30z0wyr2"
    "261sg8w7" 4 2 3  . 3 "30z0wyr2"
    "mo19or6c" 3 3 3  . 3 "30z0wyr2"
    "7unz5s7y" 1 3 3  . 3 "30z0wyr2"
    "xp5dr3wz" 3 2 4  . 3 "30z0wyr2"
    "zwd8oerg" 3 1 4  . 3 "30z0wyr2"
    "kdrn04zt" 3 3 4  . 3 "30z0wyr2"
    "kz0pah98" 2 3 4  . 3 "30z0wyr2"
    "zvg9zop4" 4 3 4  . 3 "30z0wyr2"
    "uabc3w2i" 1 3 4  . 3 "30z0wyr2"
    "hidkbh57" 4 2 4  . 3 "30z0wyr2"
    "4s8fggab" 4 1 4  . 3 "30z0wyr2"
    "mgb7nktz" 2 1 4  . 3 "30z0wyr2"
    "261sg8w7" 1 1 4  . 3 "30z0wyr2"
    "mo19or6c" 2 2 4  . 3 "30z0wyr2"
    "7unz5s7y" 1 2 4  . 3 "30z0wyr2"
    "xp5dr3wz" 1 2 5  . 3 "30z0wyr2"
    "zwd8oerg" 3 1 5  . 3 "30z0wyr2"
    "kdrn04zt" 4 1 5  . 3 "30z0wyr2"
    "kz0pah98" 2 3 5  . 3 "30z0wyr2"
    "zvg9zop4" 4 3 5  . 3 "30z0wyr2"
    "uabc3w2i" 2 1 5  . 3 "30z0wyr2"
    "hidkbh57" 3 2 5  . 3 "30z0wyr2"
    "4s8fggab" 1 1 5  . 3 "30z0wyr2"
    "mgb7nktz" 1 3 5  . 3 "30z0wyr2"
    "261sg8w7" 3 3 5  . 3 "30z0wyr2"
    "mo19or6c" 2 2 5  . 3 "30z0wyr2"
    "7unz5s7y" 4 2 5  . 3 "30z0wyr2"
    "xp5dr3wz" 1 2 6  . 3 "30z0wyr2"
    "zwd8oerg" 3 1 6  . 3 "30z0wyr2"
    "kdrn04zt" 1 1 6  . 3 "30z0wyr2"
    "kz0pah98" 2 2 6  . 3 "30z0wyr2"
    "zvg9zop4" 4 1 6  . 3 "30z0wyr2"
    "uabc3w2i" 2 1 6  . 3 "30z0wyr2"
    "hidkbh57" 4 2 6  . 3 "30z0wyr2"
    "4s8fggab" 2 3 6  . 3 "30z0wyr2"
    "mgb7nktz" 3 3 6  . 3 "30z0wyr2"
    "261sg8w7" 3 2 6  . 3 "30z0wyr2"
    "mo19or6c" 1 3 6  . 3 "30z0wyr2"
    "7unz5s7y" 4 3 6  . 3 "30z0wyr2"
    "xp5dr3wz" 4 3 7  . 3 "30z0wyr2"
    "zwd8oerg" 2 1 7  . 3 "30z0wyr2"
    "kdrn04zt" 3 2 7  . 3 "30z0wyr2"
    "kz0pah98" 3 3 7  . 3 "30z0wyr2"
    "zvg9zop4" 2 2 7  . 3 "30z0wyr2"
    "uabc3w2i" 3 1 7  . 3 "30z0wyr2"
    "hidkbh57" 4 1 7  . 3 "30z0wyr2"
    "4s8fggab" 2 3 7  . 3 "30z0wyr2"
    "mgb7nktz" 1 1 7  . 3 "30z0wyr2"
    "261sg8w7" 1 3 7  . 3 "30z0wyr2"
    "mo19or6c" 4 2 7  . 3 "30z0wyr2"
    "7unz5s7y" 1 2 7  . 3 "30z0wyr2"
    "xp5dr3wz" 1 3 8  . 3 "30z0wyr2"
    "zwd8oerg" 1 2 8  . 3 "30z0wyr2"
    "kdrn04zt" 1 1 8  . 3 "30z0wyr2"
    "kz0pah98" 3 3 8  . 3 "30z0wyr2"
    "zvg9zop4" 3 2 8  . 3 "30z0wyr2"
    "uabc3w2i" 4 2 8  . 3 "30z0wyr2"
    "hidkbh57" 2 3 8  . 3 "30z0wyr2"
    "4s8fggab" 4 3 8  . 3 "30z0wyr2"
    "mgb7nktz" 2 2 8  . 3 "30z0wyr2"
    "261sg8w7" 4 1 8  . 3 "30z0wyr2"
    "mo19or6c" 2 1 8  . 3 "30z0wyr2"
    "7unz5s7y" 3 1 8  . 3 "30z0wyr2"
    "vrgiyaxr" 2 2 1 42 1 "b7nsfswp"
    "lofv3veu" 7 3 1 34 1 "b7nsfswp"
    "4h9uvhn9" 4 2 1 30 1 "b7nsfswp"
    "bpvpb0k8" 2 3 1 60 1 "b7nsfswp"
    end

    I would highly appreciate your help or useful suggestions to create these variables.
    Last edited by Muhammad Arslan Iqbal; 02 Oct 2022, 20:27.

  • #2
    Consider this code. I changed a couple values of T1_success in your data example, to produce a situation where there was a tie in all three scores (your example had no such case).

    Code:
    clear
    input str8 participantcode byte(P2_group_id P2_player_id Round T1_success Treatment) str8 sessioncode
    "xp5dr3wz" 3 2 1 33 3 "30z0wyr2"
    "zwd8oerg" 4 1 1 45 3 "30z0wyr2"
    "kdrn04zt" 2 3 1 31 3 "30z0wyr2"
    "kz0pah98" 3 3 1 43 3 "30z0wyr2"
    "zvg9zop4" 1 2 1 31 3 "30z0wyr2"
    "uabc3w2i" 1 3 1 31 3 "30z0wyr2"
    "hidkbh57" 4 3 1 36 3 "30z0wyr2"
    "4s8fggab" 3 1 1 39 3 "30z0wyr2"
    "mgb7nktz" 4 2 1 35 3 "30z0wyr2"
    "261sg8w7" 2 2 1 36 3 "30z0wyr2"
    "mo19or6c" 1 1 1 31 3 "30z0wyr2"
    "7unz5s7y" 2 1 1 39 3 "30z0wyr2"
    "xp5dr3wz" 2 3 2  . 3 "30z0wyr2"
    "zwd8oerg" 1 2 2  . 3 "30z0wyr2"
    "kdrn04zt" 3 1 2  . 3 "30z0wyr2"
    "kz0pah98" 1 1 2  . 3 "30z0wyr2"
    "zvg9zop4" 3 3 2  . 3 "30z0wyr2"
    "uabc3w2i" 1 3 2  . 3 "30z0wyr2"
    "hidkbh57" 3 2 2  . 3 "30z0wyr2"
    "4s8fggab" 2 1 2  . 3 "30z0wyr2"
    "mgb7nktz" 4 1 2  . 3 "30z0wyr2"
    "261sg8w7" 4 3 2  . 3 "30z0wyr2"
    "mo19or6c" 4 2 2  . 3 "30z0wyr2"
    "7unz5s7y" 2 2 2  . 3 "30z0wyr2"
    "xp5dr3wz" 3 1 3  . 3 "30z0wyr2"
    "zwd8oerg" 2 3 3  . 3 "30z0wyr2"
    "kdrn04zt" 3 2 3  . 3 "30z0wyr2"
    "kz0pah98" 4 3 3  . 3 "30z0wyr2"
    "zvg9zop4" 4 1 3  . 3 "30z0wyr2"
    "uabc3w2i" 1 1 3  . 3 "30z0wyr2"
    "hidkbh57" 1 2 3  . 3 "30z0wyr2"
    "4s8fggab" 2 1 3  . 3 "30z0wyr2"
    "mgb7nktz" 2 2 3  . 3 "30z0wyr2"
    "261sg8w7" 4 2 3  . 3 "30z0wyr2"
    "mo19or6c" 3 3 3  . 3 "30z0wyr2"
    "7unz5s7y" 1 3 3  . 3 "30z0wyr2"
    "xp5dr3wz" 3 2 4  . 3 "30z0wyr2"
    "zwd8oerg" 3 1 4  . 3 "30z0wyr2"
    "kdrn04zt" 3 3 4  . 3 "30z0wyr2"
    "kz0pah98" 2 3 4  . 3 "30z0wyr2"
    "zvg9zop4" 4 3 4  . 3 "30z0wyr2"
    "uabc3w2i" 1 3 4  . 3 "30z0wyr2"
    "hidkbh57" 4 2 4  . 3 "30z0wyr2"
    "4s8fggab" 4 1 4  . 3 "30z0wyr2"
    "mgb7nktz" 2 1 4  . 3 "30z0wyr2"
    "261sg8w7" 1 1 4  . 3 "30z0wyr2"
    "mo19or6c" 2 2 4  . 3 "30z0wyr2"
    "7unz5s7y" 1 2 4  . 3 "30z0wyr2"
    "xp5dr3wz" 1 2 5  . 3 "30z0wyr2"
    "zwd8oerg" 3 1 5  . 3 "30z0wyr2"
    "kdrn04zt" 4 1 5  . 3 "30z0wyr2"
    "kz0pah98" 2 3 5  . 3 "30z0wyr2"
    "zvg9zop4" 4 3 5  . 3 "30z0wyr2"
    "uabc3w2i" 2 1 5  . 3 "30z0wyr2"
    "hidkbh57" 3 2 5  . 3 "30z0wyr2"
    "4s8fggab" 1 1 5  . 3 "30z0wyr2"
    "mgb7nktz" 1 3 5  . 3 "30z0wyr2"
    "261sg8w7" 3 3 5  . 3 "30z0wyr2"
    "mo19or6c" 2 2 5  . 3 "30z0wyr2"
    "7unz5s7y" 4 2 5  . 3 "30z0wyr2"
    "xp5dr3wz" 1 2 6  . 3 "30z0wyr2"
    "zwd8oerg" 3 1 6  . 3 "30z0wyr2"
    "kdrn04zt" 1 1 6  . 3 "30z0wyr2"
    "kz0pah98" 2 2 6  . 3 "30z0wyr2"
    "zvg9zop4" 4 1 6  . 3 "30z0wyr2"
    "uabc3w2i" 2 1 6  . 3 "30z0wyr2"
    "hidkbh57" 4 2 6  . 3 "30z0wyr2"
    "4s8fggab" 2 3 6  . 3 "30z0wyr2"
    "mgb7nktz" 3 3 6  . 3 "30z0wyr2"
    "261sg8w7" 3 2 6  . 3 "30z0wyr2"
    "mo19or6c" 1 3 6  . 3 "30z0wyr2"
    "7unz5s7y" 4 3 6  . 3 "30z0wyr2"
    "xp5dr3wz" 4 3 7  . 3 "30z0wyr2"
    "zwd8oerg" 2 1 7  . 3 "30z0wyr2"
    "kdrn04zt" 3 2 7  . 3 "30z0wyr2"
    "kz0pah98" 3 3 7  . 3 "30z0wyr2"
    "zvg9zop4" 2 2 7  . 3 "30z0wyr2"
    "uabc3w2i" 3 1 7  . 3 "30z0wyr2"
    "hidkbh57" 4 1 7  . 3 "30z0wyr2"
    "4s8fggab" 2 3 7  . 3 "30z0wyr2"
    "mgb7nktz" 1 1 7  . 3 "30z0wyr2"
    "261sg8w7" 1 3 7  . 3 "30z0wyr2"
    "mo19or6c" 4 2 7  . 3 "30z0wyr2"
    "7unz5s7y" 1 2 7  . 3 "30z0wyr2"
    "xp5dr3wz" 1 3 8  . 3 "30z0wyr2"
    "zwd8oerg" 1 2 8  . 3 "30z0wyr2"
    "kdrn04zt" 1 1 8  . 3 "30z0wyr2"
    "kz0pah98" 3 3 8  . 3 "30z0wyr2"
    "zvg9zop4" 3 2 8  . 3 "30z0wyr2"
    "uabc3w2i" 4 2 8  . 3 "30z0wyr2"
    "hidkbh57" 2 3 8  . 3 "30z0wyr2"
    "4s8fggab" 4 3 8  . 3 "30z0wyr2"
    "mgb7nktz" 2 2 8  . 3 "30z0wyr2"
    "261sg8w7" 4 1 8  . 3 "30z0wyr2"
    "mo19or6c" 2 1 8  . 3 "30z0wyr2"
    "7unz5s7y" 3 1 8  . 3 "30z0wyr2"
    "vrgiyaxr" 2 2 1 42 1 "b7nsfswp"
    "lofv3veu" 7 3 1 34 1 "b7nsfswp"
    "4h9uvhn9" 4 2 1 30 1 "b7nsfswp"
    "bpvpb0k8" 2 3 1 60 1 "b7nsfswp"
    end
    
    egen _T1_success = max(T1_success), by(participantcode)
    replace T1_success = _T1_success
    drop _T1_success
    
    local grouping session Round P2_group_id
    sort `grouping'
    
    by `grouping': egen byte rank = rank(T1_success)
    replace rank = 4-rank
    
    by `grouping': egen min_rank = min(rank)
    by `grouping': egen max_rank = max(rank)
    
    gen byte tie = (rank != int(rank)) | (min_rank == 2 & max_rank == 2)
    
    gen byte tie_all = min_rank == 2 & max_rank == 2
    gen byte tie_top = min_rank == 1.5
    gen byte tie_bottom = max_rank == 2.5
     
    by `grouping': egen rank2 = rank(P2_player_id) if tie, track
    
    gen byte wanted = rank
    replace wanted = rank2 if min_rank == 2 & max_rank == 2
    replace wanted = floor(rank) - 1 + rank2 if min_rank != max_rank & tie
    
    drop rank min_rank max_rank tie rank2
    rename wanted rank
    order rank, after(sessioncode)
    sort `grouping' rank
    This produces:
    Code:
    . li , noobs sepby(sessioncode Round P2_group_id)
    
      +---------------------------------------------------------------------------------------------------------------+
      | partic~e   P2_gro~d   P2_pla~d   Round   T1_suc~s   Treatm~t   sessio~e   rank   tie_all   tie_top   tie_bo~m |
      |---------------------------------------------------------------------------------------------------------------|
      | mo19or6c          1          1       1         31          3   30z0wyr2      1         1         0          0 |
      | zvg9zop4          1          2       1         31          3   30z0wyr2      2         1         0          0 |
      | uabc3w2i          1          3       1         31          3   30z0wyr2      3         1         0          0 |
      |---------------------------------------------------------------------------------------------------------------|
      | 7unz5s7y          2          1       1         39          3   30z0wyr2      1         0         0          0 |
      | 261sg8w7          2          2       1         36          3   30z0wyr2      2         0         0          0 |
      | kdrn04zt          2          3       1         31          3   30z0wyr2      3         0         0          0 |
      |---------------------------------------------------------------------------------------------------------------|
      | kz0pah98          3          3       1         43          3   30z0wyr2      1         0         0          0 |
      | 4s8fggab          3          1       1         39          3   30z0wyr2      2         0         0          0 |
      | xp5dr3wz          3          2       1         33          3   30z0wyr2      3         0         0          0 |
      |---------------------------------------------------------------------------------------------------------------|
      | zwd8oerg          4          1       1         45          3   30z0wyr2      1         0         0          0 |
      | hidkbh57          4          3       1         36          3   30z0wyr2      2         0         0          0 |
      | mgb7nktz          4          2       1         35          3   30z0wyr2      3         0         0          0 |
      |---------------------------------------------------------------------------------------------------------------|
      | zwd8oerg          1          2       2         45          3   30z0wyr2      1         0         0          0 |
      | kz0pah98          1          1       2         43          3   30z0wyr2      2         0         0          0 |
      | uabc3w2i          1          3       2         31          3   30z0wyr2      3         0         0          0 |
      |---------------------------------------------------------------------------------------------------------------|
      | 4s8fggab          2          1       2         39          3   30z0wyr2      1         0         1          0 |
      | 7unz5s7y          2          2       2         39          3   30z0wyr2      2         0         1          0 |
      | xp5dr3wz          2          3       2         33          3   30z0wyr2      3         0         1          0 |
      |---------------------------------------------------------------------------------------------------------------|
      | hidkbh57          3          2       2         36          3   30z0wyr2      1         0         0          1 |
      | kdrn04zt          3          1       2         31          3   30z0wyr2      2         0         0          1 |
      | zvg9zop4          3          3       2         31          3   30z0wyr2      3         0         0          1 |
      |---------------------------------------------------------------------------------------------------------------|
      | 261sg8w7          4          3       2         36          3   30z0wyr2      1         0         0          0 |
      | mgb7nktz          4          1       2         35          3   30z0wyr2      2         0         0          0 |
      | mo19or6c          4          2       2         31          3   30z0wyr2      3         0         0          0 |
      |---------------------------------------------------------------------------------------------------------------|
      | 7unz5s7y          1          3       3         39          3   30z0wyr2      1         0         0          0 |
      | hidkbh57          1          2       3         36          3   30z0wyr2      2         0         0          0 |
      | uabc3w2i          1          1       3         31          3   30z0wyr2      3         0         0          0 |
      |---------------------------------------------------------------------------------------------------------------|
      | zwd8oerg          2          3       3         45          3   30z0wyr2      1         0         0          0 |
      | 4s8fggab          2          1       3         39          3   30z0wyr2      2         0         0          0 |
      | mgb7nktz          2          2       3         35          3   30z0wyr2      3         0         0          0 |
      |---------------------------------------------------------------------------------------------------------------|
      | xp5dr3wz          3          1       3         33          3   30z0wyr2      1         0         0          1 |
      | kdrn04zt          3          2       3         31          3   30z0wyr2      2         0         0          1 |
      | mo19or6c          3          3       3         31          3   30z0wyr2      3         0         0          1 |
      |---------------------------------------------------------------------------------------------------------------|
      | kz0pah98          4          3       3         43          3   30z0wyr2      1         0         0          0 |
      | 261sg8w7          4          2       3         36          3   30z0wyr2      2         0         0          0 |
      | zvg9zop4          4          1       3         31          3   30z0wyr2      3         0         0          0 |
      |---------------------------------------------------------------------------------------------------------------|
      | 7unz5s7y          1          2       4         39          3   30z0wyr2      1         0         0          0 |
      | 261sg8w7          1          1       4         36          3   30z0wyr2      2         0         0          0 |
      | uabc3w2i          1          3       4         31          3   30z0wyr2      3         0         0          0 |
      |---------------------------------------------------------------------------------------------------------------|
      | kz0pah98          2          3       4         43          3   30z0wyr2      1         0         0          0 |
      | mgb7nktz          2          1       4         35          3   30z0wyr2      2         0         0          0 |
      | mo19or6c          2          2       4         31          3   30z0wyr2      3         0         0          0 |
      |---------------------------------------------------------------------------------------------------------------|
      | zwd8oerg          3          1       4         45          3   30z0wyr2      1         0         0          0 |
      | xp5dr3wz          3          2       4         33          3   30z0wyr2      2         0         0          0 |
      | kdrn04zt          3          3       4         31          3   30z0wyr2      3         0         0          0 |
      |---------------------------------------------------------------------------------------------------------------|
      | 4s8fggab          4          1       4         39          3   30z0wyr2      1         0         0          0 |
      | hidkbh57          4          2       4         36          3   30z0wyr2      2         0         0          0 |
      | zvg9zop4          4          3       4         31          3   30z0wyr2      3         0         0          0 |
      |---------------------------------------------------------------------------------------------------------------|
      | 4s8fggab          1          1       5         39          3   30z0wyr2      1         0         0          0 |
      | mgb7nktz          1          3       5         35          3   30z0wyr2      2         0         0          0 |
      | xp5dr3wz          1          2       5         33          3   30z0wyr2      3         0         0          0 |
      |---------------------------------------------------------------------------------------------------------------|
      | kz0pah98          2          3       5         43          3   30z0wyr2      1         0         0          1 |
      | uabc3w2i          2          1       5         31          3   30z0wyr2      2         0         0          1 |
      | mo19or6c          2          2       5         31          3   30z0wyr2      3         0         0          1 |
      |---------------------------------------------------------------------------------------------------------------|
      | zwd8oerg          3          1       5         45          3   30z0wyr2      1         0         0          1 |
      | hidkbh57          3          2       5         36          3   30z0wyr2      2         0         0          1 |
      | 261sg8w7          3          3       5         36          3   30z0wyr2      3         0         0          1 |
      |---------------------------------------------------------------------------------------------------------------|
      | 7unz5s7y          4          2       5         39          3   30z0wyr2      1         0         0          1 |
      | kdrn04zt          4          1       5         31          3   30z0wyr2      2         0         0          1 |
      | zvg9zop4          4          3       5         31          3   30z0wyr2      3         0         0          1 |
      |---------------------------------------------------------------------------------------------------------------|
      | xp5dr3wz          1          2       6         33          3   30z0wyr2      1         0         0          1 |
      | kdrn04zt          1          1       6         31          3   30z0wyr2      2         0         0          1 |
      | mo19or6c          1          3       6         31          3   30z0wyr2      3         0         0          1 |
      |---------------------------------------------------------------------------------------------------------------|
      | kz0pah98          2          2       6         43          3   30z0wyr2      1         0         0          0 |
      | 4s8fggab          2          3       6         39          3   30z0wyr2      2         0         0          0 |
      | uabc3w2i          2          1       6         31          3   30z0wyr2      3         0         0          0 |
      |---------------------------------------------------------------------------------------------------------------|
      | zwd8oerg          3          1       6         45          3   30z0wyr2      1         0         0          0 |
      | 261sg8w7          3          2       6         36          3   30z0wyr2      2         0         0          0 |
      | mgb7nktz          3          3       6         35          3   30z0wyr2      3         0         0          0 |
      |---------------------------------------------------------------------------------------------------------------|
      | 7unz5s7y          4          3       6         39          3   30z0wyr2      1         0         0          0 |
      | hidkbh57          4          2       6         36          3   30z0wyr2      2         0         0          0 |
      | zvg9zop4          4          1       6         31          3   30z0wyr2      3         0         0          0 |
      |---------------------------------------------------------------------------------------------------------------|
      | 7unz5s7y          1          2       7         39          3   30z0wyr2      1         0         0          0 |
      | 261sg8w7          1          3       7         36          3   30z0wyr2      2         0         0          0 |
      | mgb7nktz          1          1       7         35          3   30z0wyr2      3         0         0          0 |
      |---------------------------------------------------------------------------------------------------------------|
      | zwd8oerg          2          1       7         45          3   30z0wyr2      1         0         0          0 |
      | 4s8fggab          2          3       7         39          3   30z0wyr2      2         0         0          0 |
      | zvg9zop4          2          2       7         31          3   30z0wyr2      3         0         0          0 |
      |---------------------------------------------------------------------------------------------------------------|
      | kz0pah98          3          3       7         43          3   30z0wyr2      1         0         0          1 |
      | uabc3w2i          3          1       7         31          3   30z0wyr2      2         0         0          1 |
      | kdrn04zt          3          2       7         31          3   30z0wyr2      3         0         0          1 |
      |---------------------------------------------------------------------------------------------------------------|
      | hidkbh57          4          1       7         36          3   30z0wyr2      1         0         0          0 |
      | xp5dr3wz          4          3       7         33          3   30z0wyr2      2         0         0          0 |
      | mo19or6c          4          2       7         31          3   30z0wyr2      3         0         0          0 |
      |---------------------------------------------------------------------------------------------------------------|
      | zwd8oerg          1          2       8         45          3   30z0wyr2      1         0         0          0 |
      | xp5dr3wz          1          3       8         33          3   30z0wyr2      2         0         0          0 |
      | kdrn04zt          1          1       8         31          3   30z0wyr2      3         0         0          0 |
      |---------------------------------------------------------------------------------------------------------------|
      | hidkbh57          2          3       8         36          3   30z0wyr2      1         0         0          0 |
      | mgb7nktz          2          2       8         35          3   30z0wyr2      2         0         0          0 |
      | mo19or6c          2          1       8         31          3   30z0wyr2      3         0         0          0 |
      |---------------------------------------------------------------------------------------------------------------|
      | kz0pah98          3          3       8         43          3   30z0wyr2      1         0         0          0 |
      | 7unz5s7y          3          1       8         39          3   30z0wyr2      2         0         0          0 |
      | zvg9zop4          3          2       8         31          3   30z0wyr2      3         0         0          0 |
      |---------------------------------------------------------------------------------------------------------------|
      | 4s8fggab          4          3       8         39          3   30z0wyr2      1         0         0          0 |
      | 261sg8w7          4          1       8         36          3   30z0wyr2      2         0         0          0 |
      | uabc3w2i          4          2       8         31          3   30z0wyr2      3         0         0          0 |
      |---------------------------------------------------------------------------------------------------------------|
      | bpvpb0k8          2          3       1         60          1   b7nsfswp      2         0         0          0 |
      | vrgiyaxr          2          2       1         42          1   b7nsfswp      3         0         0          0 |
      |---------------------------------------------------------------------------------------------------------------|
      | 4h9uvhn9          4          2       1         30          1   b7nsfswp      3         0         0          0 |
      |---------------------------------------------------------------------------------------------------------------|
      | lofv3veu          7          3       1         34          1   b7nsfswp      3         0         0          0 |
      +---------------------------------------------------------------------------------------------------------------+
    The ranks for the last few groups look wrong, but only because in the data example, they did not have their full 3 members. It should work fine with your actual data.
    Last edited by Hemanshu Kumar; 02 Oct 2022, 22:11.

    Comment


    • #3
      Hi Himanshu, Thank you so much for the detailed reply. I have tried your code. It worked fine however the following line of code gave an error.
      Code:
       
       local grouping session Round P2_group_id  
       sort `grouping'
      The error reads:
      session ambiguous abbreviation
      r(111);

      However, I though it was because my session variable was named as sessioncode rather than just session as mentioned in your example code. So, I tried again by replacing session with sessioncode as given below however I still got another error.

      Code:
       
       local grouping sessioncode Round P2_group_id  
       sort `grouping'

      The error now reads:
      sort `grouping'
      varlist required
      r(100);

      I am not sure what is the cause of the error. Hence, I did the manual sorting by typing the name of the variables in the same order by using the sort command and it worked. Nevertheless, I will be grateful if you can hint what might be the cause of these errors. I was careful with the different quote sign and the word grouping was highlighted blue in the do file.

      Regards,

      Comment


      • #4
        Originally posted by Muhammad Arslan Iqbal View Post
        Hi Himanshu, Thank you so much for the detailed reply. I have tried your code. It worked fine however the following line of code gave an error.
        Code:
        local grouping session Round P2_group_id
        sort `grouping'
        The error reads:
        session ambiguous abbreviation
        r(111);

        However, I though it was because my session variable was named as sessioncode rather than just session as mentioned in your example code. So, I tried again by replacing session with sessioncode as given below however I still got another error.

        Code:
        local grouping sessioncode Round P2_group_id
        sort `grouping'

        The error now reads:
        sort `grouping'
        varlist required
        r(100);

        I am not sure what is the cause of the error. Hence, I did the manual sorting by typing the name of the variables in the same order by using the sort command and it worked. Nevertheless, I will be grateful if you can hint what might be the cause of these errors. I was careful with the different quote sign and the word grouping was highlighted blue in the do file.

        Regards,
        Hemanshu, sorry for the mis-spelled name in the above reply.

        Comment


        • #5
          The problem may be that at the point that Stata executed the line
          Code:
          sort `grouping'
          it did not have any macro called grouping in its memory, so it effectively tried to execute just
          Code:
          sort
          You may have tried to run that piece of code separately or interactively. If you run the full code, it should work fine. Alternatively, if you run the code piece-by-piece, use the option Execute (include) available from the top right button on the toolbar of your do-file editor, and also from the View > Do-file Editor menu.

          Comment


          • #6
            Thank you so much. It worked fine now when I run both lines of " local grouping" and "sort" together.

            Comment

            Working...
            X