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: if, in using issues with ado files: syntax bug?


From   Kevin Geraghty <kevin@ecotope.com>
To   statalist <statalist@hsphsun2.harvard.edu>
Subject   st: if, in using issues with ado files: syntax bug?
Date   Mon, 12 Mar 2012 11:11:12 -0700 (PDT)

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/


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