Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
st: Re: st: syntax : keep variables listed in ìf'
From
Maarten buis <[email protected]>
To
[email protected]
Subject
st: Re: st: syntax : keep variables listed in ìf'
Date
Tue, 13 Jul 2010 01:14:47 -0700 (PDT)
--- On Tue, 13/7/10, SCHOUMAKER Bruno wrote:
> I am using the syntax to prepare a stata command (ado
> file). I used the [if] in the syntax.
>
> I would like find an easy way to extract the names of the
> variables appearing in the macro ìf'.
I don't know if this qualifies as easy, but it seems to work:
*----------------- begin example -------------------
program drop _all
program define foo
syntax [if]
// add spaces between variables and operators
local if : subinstr local if "!" " !" , all
local if : subinstr local if "==" " ==", all
local if : subinstr local if "<" " <" , all
local if : subinstr local if ">" " >" , all
local if : subinstr local if "|" " |" , all
local if : subinstr local if "&" " & ", all
local if : list retokenize if
tokenize `if'
local k : word count `if'
forvalues i = 1/`k' {
capture confirm variable ``i''
if !_rc {
local ifvars "`ifvars' ``i''"
}
}
di "`ifvars'"
end
sysuse auto
foo if mpg > 10 & displacement!= 5
*------------------- end example -----------------------
If you were to use this code I would put it in a sub-program
to keep your code easily readable. Something like:
program define main_programe
syntax ... [if] ...
...
Findifvars if `if'
...
end
program define findifvars, sclass
<code from above>
end
Hope this helps,
Maarten
--------------------------
Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen
Germany
http://www.maartenbuis.nl
--------------------------
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/