Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Re: sorting a string variable, but not alphabetically


From   "Michael Blasnik" <[email protected]>
To   <[email protected]>
Subject   st: Re: sorting a string variable, but not alphabetically
Date   Wed, 15 Dec 2004 11:10:55 -0500

It is not completely clear how you will get around typing the full list in the order you want them to get the new sorting variable created. I would suggest that you define a value label with the ordering you want and then use the label option of the encode command:

label define animals 1"cat" 2"mouse" 3"dog" ...
encode animals, gen(animal2) label(animal)

If there is some other way to get the observations in the desired order, then you may want to check out -sencode- (type findit sencode).

Michael Blasnik
[email protected]

----- Original Message ----- From: "TEWODAJ MOGUES" <[email protected]>
Sent: Wednesday, December 15, 2004 10:51 AM
Subject: st: sorting a string variable, but not alphabetically



Hi Statalisters,

I have a question about how to sort by a string variable in a
particular way. Suppose the data looks like:

animals    ID
dog        5
cat        10
mouse      4

Suppose I want to sort by -animals- such that the first observation is
cat, the 2nd is mouse, the 3rd dog. Is there a command that may look
something like:

sort animals [some command] cat mouse dog  ?

I have used some roundabout ways to deal with this, specifically
(suppose ID has unique observations) I generated another variable by
which I sorted by, i.e.

recode ID 10=1 4=2 5=3, gen(sortvar)
sort sortvar

but this is not a great solution given that I have some 800
observations and it is also not generalisable in the sense that if the
data gets modified so that the assigned ID for each animal changes, I
have to rewrite the -recode- command. I also considered transposing the
data then ordering the animal variables but I couldn't find a way to
make stata transpose and use as its new variables the observations of
-animals-

Any thoughts on this would really be appreciated!
Tewodaj

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index