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]

Re: st: Unable to clear "invalid syntax r(197);" error in user-written .ado file


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Unable to clear "invalid syntax r(197);" error in user-written .ado file
Date   Fri, 5 Oct 2012 10:23:12 +0100

Your program accepts -if- and -in-, but does nothing about them.
-marksample- is the way to do this.

Setting aside the string default issue answered by Joseph, a revised
program might be something like

program atrim
     version 11.2
     syntax varlist(numeric) [if] [in] ///
         [, Byvar(string) Tail(real 0.5) Suffix(string)]

    quietly {

     marksample touse, novarlist
     count if `touse'
     if r(N) == 0 error 2000

    tempvar thisuse pct
    gen byte `thisuse' = .

     foreach x of local varlist {
         replace `thisuse'  = `touse' & !missing(`x')
         bysort `thisuse' `byvar': egen `pct' = rank(`x')
         bysort `thisuse' `byvar' : replace `pct' = 100 * (`pct' - 0.5) / _N
         generate `x'_`suffix' = `x' if `thisuse' & inrange(`pct',
`tail', 100 - `tail')
         drop `pct'
     }
  }

   end

On Fri, Oct 5, 2012 at 3:24 AM, Richard Herron
<richard.c.herron@gmail.com> wrote:
> I wrote an .ado file to trim data in the left and right tails, but I
> can't get past an "invalid syntax r(197)" error. My -syntax- line is
> as follows.
>
> * begin syntax line
>     syntax varlist(numeric) [if] [in] ///
>         [, Byvar(string fyear) Tail(real 0.5) Suffix(string tr)]
> * end syntax line
>
> I would like to use a -varlist- so that I can loop over the supplied
> variables, but I can't clear the error using -varname- or by dropping
> the -if- and -in- statements. I also tried making the options required
> without defaults, but that didn't work either.
>
> What is my misunderstanding of syntax? Thank you!
>
> Below is the complete .ado file. I use Stata SE 11.2 (64 bit) on Windows 7.
>
> * begin .ado file
> *! 0.1 Richard Herron 10/3/2012
> program atrim
>     version 11.2
>     syntax varlist(numeric) [if] [in] ///
>         [, Byvar(string fyear) Tail(real 0.5) Suffix(string tr)]
>
>     * rather than -bysort- twice below
>     sort `byvar'
>
>     * not necessary to tokenize; loop varlist
>     foreach x of local varlist {
>
>         * determine percentiles
>         by `byvar': egen num = count(`x') if !missing(`x')
>         by `byvar': egen ran = rank(`x') if !missing(`x')
>         generate pct = 100 * (ran - 0.5) / num
>
>         * replace tails with -sysmiss-
>         generate test = if inrange(pct, `tail', `=100 - `tail'')
>         generate `x'_`suffix' = cond(test, `x', .)
>
>         * drop "temporary" variables
>         drop num ran pct test
>
>     }
>
>     end
> * end .ado file
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


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