Hi Joseph and Nick,
Thanks to both of you for your ideas as to how to sort by a strin
variable. Joseph, your idea is a neat trick to get around the lack of
a transposing command that names the new variables according the
values of an old variable. Nick, I'm still chewing on your programming
suggestion, I think this will be helpful beyond my specific sorting
problem.
Thanks,
Tewodaj
Message: 2
Date: Wed, 15 Dec 2004 23:18:53 -0000
From: "Nick Cox" <[email protected]>
Subject: st: RE: Re: sorting a string variable, but not alphabetically
But you can do this, even though Michael's main point
remains spot on. You just need to use a mechanism for
interpreting your input as labels in a particular order
defining a sorting variable.
. l
+-----------+
| var1 |
|-----------|
1. | two names |
2. | bird |
3. | cat |
4. | dog |
5. | mouse |
+-----------+
. mystringsort var1 cat dog
. l
+-----------+
| var1 |
|-----------|
1. | cat |
2. | dog |
3. | bird |
4. | two names |
5. | mouse |
+-----------+
. mystringsort var1 "two names"
Here is a sketch:
program mystringsort
version 8
gettoken varname 0 : 0
capture confirm string var `varname'
if _rc {
di as err "`varname' should be a string variable"
exit 198
}
tempvar encoded
tempname lblname
local i = 1
foreach w of local 0 {
label def `lblname' `i++' `"`w'"', modify
}
encode `varname', gen(`encoded') label(`lblname')
sort `encoded'
end
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~
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/