Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Steve Samuels <sjsamuels@gmail.com> |
To | statalist@hsphsun2.harvard.edu |
Subject | st: Re: st: Re: st: syntax : keep variables listed in ìf' |
Date | Tue, 13 Jul 2010 04:59:40 -0400 |
Maarten, I think you need to add a space after each operator, or something like "foo if mpg>rep78: will fail. Steve -- Steven Samuels sjsamuels@gmail.com 18 Cantine's Island Saugerties NY 12477 USA Voice: 845-246-0774 Fax: 206-202-4783 On Tue, Jul 13, 2010 at 4:14 AM, Maarten buis <maartenbuis@yahoo.co.uk> wrote: > --- 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/ > * * 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/