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   Joseph Coveney <[email protected]>
To   Statalist <[email protected]>
Subject   st: Re: sorting a string variable, but not alphabetically
Date   Thu, 16 Dec 2004 09:05:53 +0900

Tewodaj wrote:

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. 

--------------------------------------------------------------------------------

Although I would favor Michael Blasnik's and Nick Cox's approach for this 
problem, you can get the animal names as variable names as follows.

Joseph Coveney


. clear

. input str10 animals byte ID

        animals        ID
  1. dog        5
  2. cat        10
  3. mouse      4
  4. end

. tostring ID, replace
ID was byte now str2

. sxpose, clear

. tempfile tmpfil

. outsheet using `tmpfil', nonames

. insheet using `tmpfil', clear
(3 vars, 1 obs)

. list, clean

       dog   cat   mouse  
  1.     5    10       4  

. // To include animals
. //                 ID
. generate str2 animals = "ID"

. order animals

. list, clean

       animals   dog   cat   mouse  
  1.        ID     5    10       4  

. exit


*
*   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