Reputation: 854
I want to generate ranks of values from lowest to highest across multiple variables in Stata. In the table below, the columns 2–4 show observed data values for variables x, y, and z, and columns 5–7 show ranks—including tied ranks—across all three variables.
Notice that "across all three variables" means that, for example, the lowest rank = 1 is applied only to the smallest value out of all three variables (i.e. only to the value 0.2 for variable x).
id | x | y | z | rank(x) | rank(y) | rank(z) |
---|---|---|---|---|---|---|
1 | 1.2 | 2.6 | 2.0 | 5 | 12 | 10.5 |
2 | 0.2 | 2.0 | 0.9 | 1 | 10.5 | 3.5 |
3 | 0.6 | 1.5 | 1.7 | 2 | 6 | 7 |
4 | 1.8 | 0.9 | 1.9 | 8 | 3.5 | 9 |
I was hoping egen
would provide a one-line kind of solution, but I think it only creates a single rank variable.
Is there a function or one-liner a la (an imagined) rankvars x y z
that would accomplish this? Or would it require writing a program to do so?
Upvotes: 0
Views: 402
Reputation: 37338
Correct: egen
creates one outcome variable at a time, and you need other code to do this. That is not a program; it could be a few lines in a do-file.
A better way would push the data into Mata and pull out the results.
* Example generated by -dataex-. For more info, type help dataex
clear
input byte id float(x y z) byte rankx float(ranky rankz)
1 1.2 2.6 2 5 12 10.5
2 .2 2 .9 1 10.5 3.5
3 .6 1.5 1.7 2 6 7
4 1.8 .9 1.9 8 3.5 9
end
rename (x y z) (v=)
reshape long v, i(id) j(which) string
egen Rank = rank(v)
reshape wide Rank v, i(id) j(which) string
rename v* *
order id x y z rankx Rankx ranky Ranky rankz
list
+----------------------------------------------------------------------+
| id x y z rankx Rankx ranky Ranky rankz Rankz |
|----------------------------------------------------------------------|
1. | 1 1.2 2.6 2 5 5 12 12 10.5 10.5 |
2. | 2 .2 2 .9 1 1 10.5 10.5 3.5 3.5 |
3. | 3 .6 1.5 1.7 2 2 6 6 7 7 |
4. | 4 1.8 .9 1.9 8 8 3.5 3.5 9 9 |
+----------------------------------------------------------------------+
Upvotes: 1