Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Options starting with "no" doesn't work, bug or feature?

    I am defining a program that has non-required options. When the option starts with "no", it doesn't work as expected. If I change it to "No" it works but is case-insensitive in the code (i.e., I can call the option by no... further down in the code).

    This is in Stata 16.1, running on Windows.

    Is there some feature of Stata I am missing out on, which is why this is proper behavior?

    See below MWE
    Code:
    cap program drop doesnt_work
    cap program drop works
    cap program drop works_but_weird
    
    program define doesnt_work
        syntax, [no_option]
        
        if "`no_option'" == "" {
            disp "no option was called ..."
        }
        else {
            disp "no-option used!"
        }
    end
    
    program define works
        syntax, [n_option]
        
        if "`n_option'" == "" {
            disp "no option was called ..."
        }
        else {
            disp "no-option used!"
        }
    end
    
    program define works_but_weird
        syntax, [No_option]
        
        if "`no_option'" == "" { // note also, case-insensitive (no v.s. No)
            disp "no option was called ..."
        }
        else {
            disp "no-option used!"
        }
    end
    
    doesnt_work            // prints "no option was called ..."
    doesnt_work, no_option // prints "no option was called ..."
    
    works                  // prints "no option was called ..."
    works, n_option        // prints "no-option used!"
    
    works_but_weird        // prints "no option was called ..."
    works_but_weird, no_option // prints "no-option used!"

  • #2
    Have you checked the help file for Stata's command syntax, especially the section for optionally off options?
    Code:
    help syntax##optionally_off
    Things will probably be cleared up there.

    Comment


    • #3
      Ah, great tip!

      So, there is a particular case of using specifically "no" at the beginning of an option. I haven't yet understood why this is introducing more functionality than confusion (in my original program I made the same functionality using conditional statements), but I'll see how I can make use of this.

      Don't know how I ever would've found this without help. But now that I know, I found the following in the Stata programming reference manual Relase 16, p. 546:
      "Warning: Be careful if the first two letters of the option’s name are no, such as the option called notice. You must capitalize at least the N in such cases."

      Thanks!
      Jesper

      Comment


      • #4
        What Joseph is showing does not lead to anything on my Stata 15.1.

        But yes, I vaguely remember there was something bizarre in the syntax of noSomething options.

        You can check the code of my -suregr-

        Code:
        ssc install suregr
        What I see in the code there is that I have specified the
        Code:
        syntax [, CLUSTER(varname) MINUS(integer 0) noHeader]
        but then when I am evoking the noHeader option, I seem to be evoking it with just header:

        Code:
        sureg, `header'
        I read about this somewhere in the -syntax- when I wrote it, but where it was I do not remember anymore.

        Comment


        • #5
          You can find the documentation in https://www.stata.com/manuals/p.pdf, page 567 also (Stata programming reference manual Release 17).

          What happens is that an option which starts with "no" will create a local named by what follows the "no" (in your case "header"). The local header will store the value "noheader".
          Code:
          program define test
             syntax, noheader
          
             disp "`header'!"
          end
          test, noheader
          
          . test, noheader
          noheader!

          Comment

          Working...
          X