Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Order variables alphabetically, not like -aorder-


From   Maarten buis <[email protected]>
To   [email protected]
Subject   Re: st: Order variables alphabetically, not like -aorder-
Date   Wed, 24 Oct 2007 22:30:12 +0100 (BST)

--- Friedrich Huebler <[email protected]> wrote:
> Assume I have a dataset with the variables year1, year2, ..., year19,
> year20.
> 
> forval i = 1/20 {
>   gen year`i' = 1
> }
> 
> The command -aorder- sorts the variables in ascending order of the
> number that follows "year". The technical note in [D] order explains
> that this is by design, "even though to most computer programs,
> year10 is alphabetically between year1 and year2."
> 
> There are cases where I would like the variables to be sorted in
> strict alphabetical order, like this:
> 
> year1
> year10
> ...
> year19
> year2
> year20
> year3
> ...
> year9
> 
> Any suggestions on how this can be done without specifying the sort
> order with -order-? It would be nice to have an option like -aorder,
> strict- that does not try to be as smart as the current version of
> -aorder-.

You can combine two tricks to do what you want, first the command
-unab- will put a unabreviated variable list in a local, e.g. 
-unab s : year*- will make a local `s' containing year1 year2 ...
year20. Than you can use an extended macro function to sort this list
strictly alphebatically, e.g. -local s : list sort s- will replace the
local `s' with year1 year10 year2 ... year9. Than you can issue a
command -sort `s'- See the example below and -help unab- and 
-help macrolists-:

*---------------- begin example ----------------
sysuse nslw88, clear
tab grade, gen(ed)
unab s: ed*
di "`s'"
local s : list sort s
di "`s'"
order `s'
*----------------- end example -----------------
(For more on how to use examples I sent to the Statalist, see
http://home.fsw.vu.nl/m.buis/stata/exampleFAQ.html )

Hope this helps,
Maarten

-----------------------------------------
Maarten L. Buis
Department of Social Research Methodology
Vrije Universiteit Amsterdam
Boelelaan 1081
1081 HV Amsterdam
The Netherlands

visiting address:
Buitenveldertselaan 3 (Metropolitan), room Z434

+31 20 5986715

http://home.fsw.vu.nl/m.buis/
-----------------------------------------


      ___________________________________________________________
Yahoo! Answers - Got a question? Someone out there knows the answer. Try it
now.
http://uk.answers.yahoo.com/ 
*
*   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