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
So you must specify
1. the variable name
2. the order of items
noting that
3. items with embedded spaces need " "
4. this is going to be literal
5. things not specified go to end.
Nick
[email protected]
> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]]On Behalf Of
> TEWODAJ MOGUES
> Sent: 15 December 2004 22:52
> To: Stata _
> Subject: st: Re: sorting a string variable, but not alphabetically
>
>
> 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-
> >
*
* 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/