Announcement

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

  • Program syntax, pass generic options through to twoway

    I'm new to writing programs (functions) in stata, so I'm looking for both a specific answer to this question, and a generic answer for locations/terms to search for on related topics.

    My specific question is this:
    I'm writing programs to make minor variations on complex plots. I'd like to be able to set up the general plot in a program and pass options through to the tw graphics plotter, rather than having to hard code all the options.

    here is a minimal working example:

    Code:
    capture program drop toy_syntax
    program define toy_syntax
    
    syntax varlist(max =2) [if], ms(string) [nodraw(string)]
    
    tw (scatter `1' `2') `if', title("`ms'") `nodraw'
    
    end
    
    toy_syntax var1 var2 if var3 < 100, ms("mwe") nodraw("nodraw")



    This strategy _works_ to pass the nodraw option through to tw, but its inelegant, and I'm sure that there is some general way to just send all of the default plotting options into a program. How do I do this more elegantly than my current solution?




    A more general question:
    I'm aware of the formal stata documentation, but I'm having a hard time making progress in googling questions about writing stata programs, because the terms are so general (stata, program, options doesn't get me very far). I've read the manual https://www.stata.com/manuals13/u18.pdf carefully, and occasionally find useful posts here or on stack exchange (thanks Nick Cox!), but are there other places I can look to address ongoing questions about how to write programs or find example code about programming stata?


    Thank you!

  • #2
    On the specific question, here are two approaches

    Code:
    program toy_syntax
        version 15
        
        syntax [ , MYOPT * ]
        
        display "`options'"
    end
    Here is how this works out

    Code:
    . toy_syntax , myopt all other options such as nodraw
    all other options such as nodraw
    When you use the asterisk (*), the local macro options contains all options that you do not explicitly specify (or: anything that is not recognized as an option for toy_syntax). You can pass these options to twoway.

    Alternatively, you can

    Code:
    program toy_syntax2
        version 15
        
        syntax [ , MYOPT TWopts(string asis) ]
        
        display `"`twopts'"'
    end
    which gives

    Code:
    . toy_syntax2 , myopt tw(any options for twoway such as nodraw)
    any options for twoway such as nodraw
    Specify an option, twopts(), that the user will have to specify explicitly and pass its contents to twoway.

    If you want to set defaults, however, there is no other way than specifying all options for which you want to set defaults explicitly and process them one at a time.

    On the more general question, think about purchasing Kit's book.

    Best
    Daniel
    Last edited by daniel klein; 16 May 2018, 08:57. Reason: use compound double quotes in toy_syntax2

    Comment

    Working...
    X