1
votes

I am relatively new to writing ado files in Stata and have come across a problem that I cannot find a solution for.

I would like to have an option in my program that allows any twoway option to be used (see help twoway_options) and have run into a problem when using quotes either with just whitespace or within subcommands as demonstrated below.

sysuse gnp96, clear

capture prog drop adding_quotes
prog def adding_quotes 

    syntax [, tw_opts(string)]

    line gnp96 date, `tw_opts'

end

// throws error
adding_quotes, tw_opts(text(7000 97 "Middle Text"))
adding_quotes, tw_opts(xtitle(" "))

// runs
adding_quotes, tw_opts(text(7000 97 `""Middle Text""'))
adding_quotes, tw_opts(xtitle(""))

I would also note that doing away with the syntax command will also solve the problem, but I would rather keep it in and not have to parse the whole command.

Is it possible to change the syntax command so that the two commands that throw errors work?

1
I have stumbled across a second solution in addition to Nick's that might be better for certain situations, instead of using tw_opts(string) if one uses tw_opts(string asis) the resulting local tw_opts will take the input as is from the arguments and be able to run any of the above.Eric HB
Good, but that was my point #3.Nick Cox

1 Answers

1
votes

Two suggestions and a comment:

1) You want to allow any twoway options, which you will pass to a graph command. It's simplest just to use a wildcard * in syntax and let syntax (and in this case twoway) do all the work.

sysuse gnp96, clear

capture prog drop adding_quotes
prog def adding_quotes 
    syntax [, * ]
    line gnp96 date, `options'
end

adding_quotes, text(7000 97 "Middle Text") 
adding_quotes, xtitle(" ")

2) As above, the xlabel(" ") call was illegal regardless. You're probably influenced by terminology elsewhere to think of what Stata calls axis titles as axis labels. For Stata, the axis labels are the individual text labels, by default in twoway at various numeric positions on the axes.

3) In other problems specifying that an option argument is string asis inhibits the stripping of double quotes.