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   TEWODAJ MOGUES <[email protected]>
To   Stata _ <[email protected]>
Subject   st: Re: sorting a string variable, but not alphabetically
Date   Wed, 15 Dec 2004 17:52:12 -0500

Joseph,
I would be happy to transpose using sxpose, but if sxpose is simply the 
string version of xpose, it won't do the trick because it wouldn't make 
the animals into variables. I.e. transposing would generate:

var1     var2 var3 var4
animals  dog  cat  mouse
ID       5     10    4

instead of what I would want, which would be

animals  dog  cat  mouse
ID       5     10    4

where the first row is the row of variable names. 

Michael, it seems like your suggestion is the only feasible one, though 
I guess I would have preferred to be able to simply list the string 
values in desired order to execute the command. Meaning, I would have 
liked to do something like:
-some command- animals -some other command- cat mouse dog

Thanks to both of you,
Tewodaj

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

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
>

~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~
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