Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Re: st: syntax : keep variables listed in ìf'


From   Maarten buis <maartenbuis@yahoo.co.uk>
To   statalist@hsphsun2.harvard.edu
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/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index