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

st: sorting a string variable, but not alphabetically


From   TEWODAJ MOGUES <[email protected]>
To   Stata _ <[email protected]>
Subject   st: sorting a string variable, but not alphabetically
Date   Wed, 15 Dec 2004 10:51:27 -0500

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


 
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~
Tewodaj Mogues
Dept. of Agricultural and Applied Economics
University of Wisconsin - Madison
427 Lorch St. #317, Taylor Hall
Madison, WI 53706
*
*   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