Announcement

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

  • Sort country identifier according to ranking

    Dear all,
    I have the following data. I want to replace the entries of rank_y_628 by the country identifiers (icode_imf) according to the ranking given by rank_y_628.

    Code:
         +------------------------------+
         | icode_~f   rank~628   obsnum |
         |------------------------------|
      1. |      111          9        1 |
      2. |      112         16        2 |
      3. |      122         31        3 |
      4. |      124         29        4 |
      5. |      128         20        5 |
         |------------------------------|
      6. |      132         25        6 |
      7. |      134         12        7 |
      8. |      138         33        8 |
      9. |      142         22        9 |
     10. |      144         32       10 |
         |------------------------------|
     11. |      146         34       11 |
     12. |      156         28       12 |
     13. |      158          2       13 |
     14. |      172         17       14 |
     15. |      174         18       15 |
         |------------------------------|
     16. |      178         30       16 |
     17. |      182         35       17 |
     18. |      184         21       18 |
     19. |      186          6       19 |
     20. |      193         14       20 |
         |------------------------------|
     21. |      196          8       21 |
     22. |      199          5       22 |
     23. |      273         26       23 |
     24. |      528          3       24 |
     25. |      532          4       25 |
         |------------------------------|
     26. |      534          1       26 |
     27. |      536         19       27 |
     28. |      542          7       28 |
     29. |      548         27       29 |
     30. |      566         24       30 |
         |------------------------------|
     31. |      576         11       31 |
     32. |      578         10       32 |
     33. |      935         15       33 |
     34. |      944         23       34 |
     35. |      964         13       35 |
    I tried to achieve that with the following code.

    Code:
    local count=_N
    forval i=1/`count'{
    local hold=rank_y_628[`i']
    replace rank_y_628=icode_imf[`i'] if `hold'==obsnum
    }
    Somehow though, this does not work for all observations. More specifically, I receive the following output:

    Code:
         +----------+
         | rank~628 |
         |----------|
      1. |        9 |
      2. |      158 |
      3. |      528 |
      4. |       29 |
      5. |      199 |
         |----------|
      6. |      186 |
      7. |      542 |
      8. |      196 |
      9. |      111 |
     10. |       32 |
         |----------|
     11. |       34 |
     12. |      134 |
     13. |      964 |
     14. |       17 |
     15. |       18 |
         |----------|
     16. |      112 |
     17. |      172 |
     18. |      174 |
     19. |      536 |
     20. |      128 |
         |----------|
     21. |        8 |
     22. |        5 |
     23. |       26 |
     24. |      566 |
     25. |      132 |
         |----------|
     26. |      273 |
     27. |       19 |
     28. |        7 |
     29. |      124 |
     30. |       24 |
         |----------|
     31. |      122 |
     32. |      144 |
     33. |      138 |
     34. |      146 |
     35. |       13 |

    I'd be grateful for an explanation what went wrong and how I could fix this issue.

    Kind regards,
    Florian

    P.S. Unfortunately I'm not able to use dataex since I'm using Stata 15.0 and cannot access user-written commands.


    Last edited by Florian Schoner; 28 May 2018, 02:19.
Working...
X