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

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

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

--- Friedrich Huebler <> 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 )

Hope this helps,

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

Yahoo! Answers - Got a question? Someone out there knows the answer. Try it
*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index