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 at the end of May, and its replacement, statalist.org is already up and running.


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

Re: st: RE: if, in using issues with ado files: syntax bug?


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: RE: if, in using issues with ado files: syntax bug?
Date   Mon, 12 Mar 2012 18:37:57 +0000

What you can also do is pass your -if- and -in- conditions as options.
That way, they can used when you need them.

Nick

On Mon, Mar 12, 2012 at 6:26 PM, Nick Cox <n.j.cox@durham.ac.uk> wrote:
> This isn't a bug in -syntax-. It's intended behaviour, or a result of it. It's -syntax-'s job to compare
>
> (1) what is supplied as if it were a varlist
>
> with
>
> (2) what variables are in memory when the program in question is called
>
> _if and only if_
>
> a varlist is also specified to -syntax- as part of what should or could be supplied. It's doing what you asked.
>
> Note that there's no rule that -syntax- must be fed varlist as one of the elements of a legal program call.
>
> What is in -using()-'s argument is a different question. Of course, there are occasions like this when what you care about what is in the using file, but you have to program accordingly.
>
> What you need here, I think, is a namelist.
>
> Nick
> n.j.cox@durham.ac.uk
>
> Kevin Geraghty
>
> consider the following example .ado file, which uses standard stata syntax:
>
>
> program define maxval, rclass
>    syntax varname [if] [in] [using]
>    version 12
>
>    preserve
>        if `"`using'"' !=`""' {
>            use `using', clear
>        }
>
>        collapse (max) `varlist' `in' `if'
>        return local max = `varlist'[1]
>    restore
>
> end
>
>
> It is supposed to return the max of a specified numeric variable, subject to any "if" or "in"
>  conditions, in the specified "using" data set or the current data set if no "using" data set is specified.
>
> Problem is, it doesn't work as intended because the syntax checker checks the validity of the varlist, and of the "if" and "in" conditions, against the in-memory data set rather than the "using" data set.
>
> That is, suppose you have a data set on disk called "test.dta", containing a numeric variable "fu". If you invoke your ado file
>  "maxval fu using test" it will throw an error complaining that "fu" is not defined.  If, by pure chance, you have an in-memory data set with the variable "fu" defined, the code will work as intended, that is the program's return value will be the max of "fu" in test.dta, not the max of in-memory "fu". The same is true of the "in" and "if" qualifiers, that is, suppose your in-memory data set contains ten records, but "test.dta" contains 50.  If you invoke "maxval fu in 30/50 using test" it will throw an error complaining about observation numbers out of range.
>
> This strikes me as incorrect behavior. As a stata bug. Does anybody disagree? Am I missing something?
>
>
> *
> *   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index