Statalist


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

st: RE: Re: reverse variable order


From   "Buzz Burhans" <buzzb3@verizon.net>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Re: reverse variable order
Date   Tue, 17 Jul 2007 07:53:05 -0600

Kit,

Thanks very much for your response.  

I can follow what you did with the variables, but I can't get it to work.  I
think my failure to implement it is because I have never used mata.  I am
away from my office for an extended period, and don't have my mata manual
with me. If you had time, I am interested in knowing how to run your
approach from inside a do file.

When I put it into a do file, I got the following error messages, and the
line -di "`revvars'"- failed. I thought perhaps your mata section was meant
to be run interactively, but interactively I still got an error when I got
to the line:

-v2=invtokens(v[cols(v)..1])- 


Here is what I got:

.  sysuse auto, clear 
(1978 Automobile Data)
r; t=0.00 7:46:57

.  forvalues i = 1/10 {
  2.         gen x`i' = uniform()
  3. }
r; t=0.02 7:46:57

. 
.  unab vars : x*
r; t=0.00 7:46:57

. 
. // this places the list of x* variables in macro vars
. 
.  mata
------------------------------------------------- mata (type end to exit)
------------------------------------
:  v=tokens(st_local("vars"))

: 
:  v2=invtokens(v[cols(v)..1])
                 <istmt>:  3499  invtokens() not found
r(3499);

: 
:  st_local("revvars",v2)
                 <istmt>:  3499  v2 not found
r(3499);

: 
: 
: 
:         v2
                 <istmt>:  3499  v2 not found
r(3499);

: 
:  end
----------------------------------------------------------------------------
----------------------------------
r; t=0.05 7:46:57

. 
.  di "`revvars'"

r; t=0.00 7:46:57

. 
end of do-file
r; t=0.06 7:46:57


Buzz


Buzz Burhans, Ph.D.

Dairy-Tech Group
Twin Falls, ID
Phone: 208-320-0829
Fax: 208-735-1289 

Email: buzzb3@verizon.net

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Kit Baum
Sent: Tuesday, July 17, 2007 5:58 AM
To: statalist@hsphsun2.harvard.edu
Subject: 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/

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index