Announcement

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

  • Possible bug when programming options with -program define-?

    Hi all,

    When I was coding something custom using -program define-, I encountered some behavior I didn't expect. When an option that begins with "no" is fed into a program, it seems to be treated as a logical value rather than, say, a string. Interestingly, you can't begin a string with "N" or just use "no" by itself as an option. Is this just a bug or am I missing something here?

    Some example code that replicates the issue:

    Code:
    program drop _all
    
    program define test_prog1
        syntax [anything], [test]
        display "`=("`test'" != "")'"
    end
    test_prog1, test
    
    program define test_prog2
        syntax [anything], [noo]
        display "`=("`noo'" != "")'"
    end
    test_prog2, noo
    
    program define test_prog3
        syntax [anything], [uno]
        display "`=("`uno'" != "")'"
    end
    test_prog3, uno
    
    program define test_prog4
        syntax [anything], [N]
        display "`=("`N'" != "")'"
    end
    cap test_prog4, N    //"option N not allowed"
    
    program define test_prog5
        syntax [anything], [nO]
        display "`=("`nO'" != "")'"
    end
    test_prog5, nO
    
    program define test_prog6
        syntax [anything], [no]
        display "`=("`no'" != "")'"
    end
    cap test_prog6, no    //"invalid syntax"

  • #2
    Without looking at your example, I can assure you that this is the intended behavior.

    From

    Code:
    help syntax##optionally_on
    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.
    Also, read the following paragraph on optionally-off options carefully; local macro names might differ from what you expect.

    Comment


    • #3
      Another, often overlooked but incredibly helpful detail from your last example:

      Code:
      . test_prog6 , no
      invalid syntax
      r(197);
      Note the return code: 197. This is not the usual return code for invalid syntax, which is 198. The documentation tells us that

      [P] error . . . . . . . . . . . . . . . . . . . . . . . . Return code 197
      invalid syntax
      This error is produced by syntax and other parsing commands when
      there is a syntax error in the use of the command itself rather
      than in what is being parsed.
      which implies that

      Code:
      . test_prog6
      invalid syntax
      r(197);
      (note missing option) produces the same error.


      Edit:

      Not sure what you mean by

      Originally posted by William Liu View Post
      you can't begin a string with "N"
      Code:
      . program foo
        1.     version 17
        2.     syntax , Notaproblem
        3. end
      
      .
      . foo , notaproblem
      works fine for me.

      Edit 2:

      Sorry, I missed the respective example. Stata uses capitalization in options to denote minimal abbreviation. Options cannot start with any capital letter.*

      Code:
      . program bar
        1.     version 17
        2.     syntax [ , Bar ]
        3. end
      
      . 
      . bar , Bar
      option Bar not allowed
      r(198);
      Note return code 198, meaning the syntax command is fined; the user input is the problem.


      * There are workarounds for that; but why would you want that?
      Last edited by daniel klein; 26 Apr 2022, 07:49.

      Comment


      • #4
        I see – thanks for your help!

        Comment

        Working...
        X