Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | "Peci, Adriana (OAHPP)" <Adriana.Peci@oahpp.ca> |
To | <statalist@hsphsun2.harvard.edu> |
Subject | RE: st: FW: Ranking a string variable |
Date | Mon, 23 Jan 2012 14:52:41 -0500 |
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: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Dimitriy V. Masterov Sent: January 23, 2012 1:21 PM To: statalist@hsphsun2.harvard.edu 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) <Adriana.Peci@oahpp.ca> 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. > > Can anyone help me with that ? > > > Thank you so much, > > > Adriana Peci > > * > * 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/ * * 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/ * * 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/