Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: st: FW: Ranking a string variable

From   Nick Cox <>
To   "''" <>
Subject   RE: st: FW: Ranking a string variable
Date   Mon, 23 Jan 2012 18:53:46 +0000

One interpretation of this, which I don't find at all clear, is that you seem to be expecting -egen, rank()- to produce counts and then rank the counts. 

It doesn't do that, even with numeric variables. You need to produce counts (frequencies) before you can rank them. 

I suggest you show us a fragment of your dataset, or something with similar structure, showing also what values your ranks should have. Then it should be easy for fluent Stata users to suggest code. At the moment what you want is not obvious. 


Peci, Adriana (OAHPP)

I tried but it ranked items by time rather than by department. 



-----Original Message-----
From: [] On Behalf Of Dimitriy V. Masterov
Sent: January 23, 2012 1:21 PM
Subject: Re: st: FW: Ranking a string variable

Does "bysort department time: gen rank=_n" do the trick?


On Mon, Jan 23, 2012 at 1:11 PM, Peci, Adriana (OAHPP)
<> wrote:
> Hello stata listers,
> I need some help to rank a string variable by two other variables. My
> data are in a long form in which one observation is listed more than
> once.
> The variable of interest is Item purchased  which I would like to rank
> based on the Department that this item belongs to and
> The time whern this item was purchased.
> So if departments categories are A, B, C, D etc (string variable) which
> are listed more than once in the database and the items are unique
> values as  x, y, z,.(string variable) etc.
> I want to count items of each department by purchased time as 1, 2, 3,
> 4, 5. So in case of two items were purchased at the same time I want
> them to be randomly ranked as 1, 2 and not 1,1.
> I am trying to use the egen command:  bysort department time: egen rank
> =rank(item) but it doesn't work for a string variables.

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index