# Re: st: interval variable to rank variable

 From Eva Poen To statalist@hsphsun2.harvard.edu Subject Re: st: interval variable to rank variable Date Fri, 17 Apr 2009 22:28:03 +0100

```<>

Austin,

very good catch, thank you. Indeed, a -sort- can do a lot of damage,
and I should have left it out. While writing the reply I forgot that,
of course, -egen- is clever and will automatically create groups in
ascending order of v1, without altering the sort order of the data.
Therefore, one line is enought to solve Alexey's problem.

Eva

2009/4/17 Austin Nichols <austinnichols@gmail.com>:
> Eva--
> Good point--I am so used to using -egen-'s -group()- function for
> string variables, that solution did not occur to me.  But the prior
> -sort- is superfluous, and changing the sort order can be dangerous
> for downstream code, when the current sort order contains useful
> information (hence my extra lines restoring current sort order).
>
> On Fri, Apr 17, 2009 at 4:59 PM, Eva Poen <eva.poen@gmail.com> wrote:
>> <>
>>
>> Alexey,
>>
>> what you want is not technically a rank. -egen, rank- will give you
>> ranks, taking all observations into account, and resolving ties. What
>> you want can be done with -egen- also:
>>
>> sort v1
>> egen rank = group(v1)
>>
>>
>> Hope this helps,
>> Eva
>>
>>
>>
>>> Dear all,
>>>
>>> I have an interval variable v1 with values 1,1,15,15, 47, 47, etc. and I
>>> want to recode it into a rank variable v2 so that 1=1, 15=2, 47=3, etc. Is
>>> there any simple way to do this automatically? I have tried -egen rank,
>>> track-, but it returns v3.
>>>
>>>    +------------+
>>>    | v1  v2  v3 |
>>>    |------------|
>>>  1. | 1   1   1  |
>>>  2. | 1   1   1  |
>>>  3. | 15  2   3  |   4. | 15  2   3  |   5. | 47  3   5  |   6. | 47  3   5
>>>  |
>>>    +------------+
>>>
>>> Many thanks!
>>>
>>> Alexey Bessudnov
>>> DPhil student in sociology
>>> St.Antony's College, Oxford University
>

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```