Bookmark and Share

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


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

RE: st: FW: Ranking a string variable


From   Nick Cox <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   RE: st: FW: Ranking a string variable
Date   Mon, 23 Jan 2012 20:10:44 +0000

What is good is that you solved your problem. 

FWIW, I wouldn't call this "ranking". It is a matter of producing distinct identifiers. 

More importantly, note that it seems that your data are in -department time- order only because of a prior -sort-. For this to be reproducible you would need  

. bysort department (time) : gen rank = _n 

and not just the last command you typed. 

Nick 
[email protected] 

Peci, Adriana (OAHPP)

Maybe I wasn't that clear but this is what I got after I run the "bysort department time: gen rank=_n" code.
Ranking by time within the department instead of ranking by department and time at the same time.   

+-----------------------------------------------+
      | department       time        item        rank |
      |-----------------------------------------------|
4988. | 2270-2010-149   26 Dec 10   10C0835920      1 |
4989. | 2270-2010-149   26 Dec 10   10C0835921      2 |
4990. | 2270-2010-149   28 Dec 10   10C0835943      1 |
4991. | 2270-2010-149   28 Dec 10   10C0835944      2 |
4992. | 2270-2010-149   28 Dec 10   10C0835942      3 |
      |-----------------------------------------------|
4993. | 2270-2010-149   28 Dec 10   10C0835945      4 |
+-----------------------------------------------+ 

After applying that I thought I was too close to the right solution. 
So I tried to sort by department and time first and then I run 
the "bysort department: gen rank=_n" code only. It seems that it worked.
  +------------------------------------------------+
      | department      time        item         rank2 |
      |------------------------------------------------|
4988. | 2270-2010-149   26 Dec 10   10C0835920       1 |
4989. | 2270-2010-149   26 Dec 10   10C0835921       2 |
4990. | 2270-2010-149   28 Dec 10   10C0835944       3 |
4991. | 2270-2010-149   28 Dec 10   10C0835942       4 |
4992. | 2270-2010-149   28 Dec 10   10C0835943       5 |
      |------------------------------------------------|
4993. | 2270-2010-149   28 Dec 10   10C0835945       6 |
      +------------------------------------------------+

Thank you so much for your help,

Adriana

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

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

DVM

On Mon, Jan 23, 2012 at 1:11 PM, Peci, Adriana (OAHPP)
<[email protected]> 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:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


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