Announcement

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

  • syntax for options within options?

    Hi,

    I am wondering if, and how if it does, Stata parses the syntax within options. I'm writing a command for models that take selection equations as options, and I am wondering how to go about allowing options for the selection equation itself.

    So let's say that I have the following:
    Code:
    syntax varlist(numeric fv) [if] [in] [fweight pweight iweight] [, SELect(varlist numeric fv, noCONStant HET(varlist numeric fv))]
    Does Stata somehow parse the options within select or do I have to use gettoken to parse it to see what options are in it? If Stata parses it, how do I get to the options of the option?

    Thanks!!!
    Alfonso Sanchez-Penalver

  • #2
    Whatever syntax allows is parsed for you. I would be surprised if it allowed that. My best advice would be to declare that select() allows a string argument, and then send that argument if specified to a subprogram with its own syntax statement. That subprogram could usefully be s-class letting you pass its results back to your main program.

    Comment


    • #3
      Alfonso,

      there is no direct documented way of parsing sub-options. You might find some undocumented tools used for parsing the graph commands, but I would not rely on it. However, you do not need to use gettoken either, doing it completely manually, but simply call syntax repeatedly. You could write something like

      Code:
      program mycmdname
          version 12.1
          
          syntax varlist(numeric fv) [if] [in] [fweight pweight iweight] ///
          [ , ///
              SELect(string) ///
          ]
          
          if ("`select'" != "") {
              parse_select_opt `select'
              local constant `s(constant)'
              local het `s(het)'
          }
      end
      
      program parse_select_opt , sclass
          version 12.1
          
          syntax varlist(numeric fv) ///
          [ , ///
              noConstant ///
              HET(varlist numeric fv) ///
          ]
          
          sreturn local constant `constant'
          sreturn local het `het'
      end
      There are other ways than using s() to get the parsed contents back to the caller. You could also reset local 0 in the caller itself, like

      Code:
      program mycmdname
          version 12.1
          
          syntax varlist(numeric fv) [if] [in] [fweight pweight iweight] ///
          [ , ///
              SELect(string) ///
          ]
          
          /*
              Make sure to collect all locals returned
              by the syntax command before calling it again
          */
          
          local first_varlist `varlist'
          local if_qualifier `if'
          ...
          
          if ("`select'" != "") {
              local 0 `select'
              syntax varlist(numeric fv) ///
              [ , ///
                  noConstant ///
                  HET(varlist numeric fv) ///
              ]
          }
      end
      As you see, this latter approach has some drawbacks, like having to collect all the locals returned by the first syntax command, that would be otherwise overwritten by the second call. I also find it harder to debug than having a separate sub-routine. Depending on how specific you want the error messages to be, you might need a little extra coding in both approaches.

      Best
      Daniel
      Last edited by daniel klein; 06 Nov 2015, 07:59. Reason: Well, Nick explained the code in words ...

      Comment


      • #4
        Hi Nick, Daniel,

        thank you so much for your help. I like your first approach Daniel, which picks up on Nick's suggestion to use an sclass program.
        Alfonso Sanchez-Penalver

        Comment


        • #5
          Hallo Statalist,

          I was looking for a way to include suboptions in an ado-file and came across this post, which was very helpful.
          Thanks Nick Cox and daniel klein

          I was playing around and came up with a (slightly modified) example. I want to leave it here for documentation.

          Initially I had 2 options "round100" and "combined". Round100 is used to correct rounding errors, to ensure that the sum equals 100%. "combined" is a modification, which is useful in some cases.

          Code:
          program define item                                                      // Programm "item" anlegen
           version 18
          
           syntax varlist [if] [fw aw iw], [round100] [combined]
          
          end
          Using 2 seperate options was ok, I was wonderimg how to create a subotion. The idea is to "allows a string argument, and then send that argument if specified to a subprogram with its own syntax statement." Here, the syntax/example:

          Code:
          capture program drop mycmdname
          capture program drop parse_round100_opt
          
          sysuse auto, clear
          
          program mycmdname
              version 18
          
              syntax varlist(numeric fv) [if] [in] [fweight pweight iweight] ///
              [ ,                     ///
                  round100(string)     ///
              ]
            
           
              if ("`round100'" != "") {
                  parse_round100_opt `round100'
                  local combined `s(combined)'
              }
          
          
          * Tests
              if `"`round100'"' == "" {
                  di in red "Option 'round100' NOT used!        
              }    
              
              if `"`combined'"' == "" {
                  di in red "SubOption 'combined' NOT used!        
              }
          
              if `"`round100'"' != "" {
                  di in red "Option 'round100' is used!        
              }    
          
              if `"`combined'"' != "" {
                  di in red "SubOption 'combined' is used!        
              }
          
          * contents mycmdname.ado
          * ...    
              
          end
          Code:
          program parse_round100_opt , sclass
              version 18
            
              syntax [name]     ///
              [ ,                         ///
                  combined                 ///
              ]
            
              sreturn local combined `combined'
          end
          Code:
          mycmdname price mpg
          
          mycmdname price mpg, round100(yes)
          
          mycmdname price mpg, round100(yes, combined)
          Last edited by Simon Pfaff; 14 May 2025, 04:59.

          Comment

          Working...
          X