Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: expanding a local that contains a list or wildcards


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: expanding a local that contains a list or wildcards
Date   Tue, 5 Aug 2003 19:03:24 +0100

Dimitriy V. Masterov
> 
> I am having the following problem. I have a program that 
> uses a local
> rhsvars that looks like  "x1 x2 x3 x4 x5". I would like to be able
> to use a shortcut and write it as "x1-x5" or as "x*". 
> However, this runs
> into the problem that when I tokenize this local later on in the
> program, it does not create locals 1-5 containing x1 
> through x5, just a
> local named 1 that contains the text "x1-x5". Is there a 
> way to expand
> rhsvars from its abbreviated form and redefine so that the tokenize
> command works?
> 
> I am not sure if there is a command that already does this, 
> but I could
> not find it in the manual or online. I will be very 
> grateful if someone could help me out.

One way to do this is to use -unab-. This sounds best
for your problem. 

unab rhsvars : `rhsvars' 

How does this work? Think from right to left: 

1. Stata sees `rhsvars', in your case perhaps x1-x5

2. It unabbreviates it to a expanded varlist

3. It puts that in the local named.  

In this example I used the same name for input 
and output, but that it is not compulsory. 

Another way to do it, more general than you need 
in this case, is to use -syntax-.

-syntax- works on what is in local macro 0. Usually, 
that is produced automatically as whatever was provided 
as the argument(s) to your program. On occasion, it is 
useful to assign it explicitly somewhere in the body 
of a program. 

local 0 `rhsvars' 
syntax varlist 
local rhsvars "`varlist'" 

This is like a washing machine. 

1. You put into local macro 0 your -rhsvars-. 

2. The washing machine -syntax- works on local macro 0. 
You tell it that the only legal thing it can expect 
is a varlist. 

3. Accordingly if you supplied appropriate input, 
the local macro -varlist- will afterwards contain
your expanded varlist. 

This is more complicated, but more general. 

Nick 
n.j.cox@durham.ac.uk 

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