![Statalist](/includes/images/head_statalist.jpg) |
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
st: Re: reverse variable order
To somewhat simplfy Maarten's code (without the check for x following
a strict pattern, which in fact is not required in this approach),
see below
On Jul 17, 2007, at 2:33 AM, Maarten wrote:
*---------- begin example --------------
sysuse auto, clear
forvalues i = 1/10 {
gen x`i' = uniform()
}
unab vars : x*
// this places the list of x* variables in macro vars
. mata
: v=tokens(st_local("vars"))
: v2=invtokens(v[cols(v)..1])
: st_local("revvars",v2)
:
: v2
x10 x9 x8 x7 x6 x5 x4 x3 x2 x1
: end
------------------------------------------------------------------------
--------
. di "`revvars'"
x10 x9 x8 x7 x6 x5 x4 x3 x2 x1
// Back in Stata, the local macro revvars now contains the reversed
list of x variables. No need to loop. -order- can be applied to this
list (prepended with the list of non-x variables, extracted as
Maarten illustrates).
Kit
*
* 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/