Bookmark and Share

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: Re: st: syntax : keep variables listed in ìf'


From   Steve Samuels <[email protected]>
To   [email protected]
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
[email protected]
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 <[email protected]> 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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index