--As a followup, Hung-Jen Wang <[email protected]> wrote:
[...]
> The only quirk left though
> is that the program does not accept space as a legitimate empty option
> value. That is, suppose I want the option value of -myopt- to be
> empty, myexpl, myopt()
>
> is fine, but
> myexpl, myopt( )
>
> isn't (note the space in the option). Stata actually treats it as a
> non-empty option value, resulting in undesired parsing which returns
> ")" (no quotes) as the parsed value of -myopt-.
And Hung-Jen suggested
> To fix the small oddity, after
>
> tokenize `myopt', parse("()")
> local myopt `3'
>
> I can do the check again,
>
> if "`myopt'" == ")"{
> local myopt ""
> }
Hung-Jen's suggestion is fine. I modified my original program as follows:
program define myexpl
version 6
syntax [, myopt(passthru)]
if "`myopt'" != "" {
tokenize `myopt', parse("()")
if "`3'" != ")" {
local myopt `3'
unab myopt : `myopt'
confirm numeric var `myopt'
summ `myopt'
}
else {
local myopt
}
}
if "`myopt'" == "" {
di "myopt() is empty"
}
end
Note that besides the check for ")", I also added two other commands.
1) -unab- not only unabbreviates the varlist, but also helps to check whether
the variables specified are valid. If the user mistypes a variable name which
is not in the memory, the program will exit with error.
. myexpl, myopt( pr mpg notexist)
notexist not found
r(111);
2) -confirm- helps to confirm the variables are numeric. This is optional and
can be modified to check other types of variables.
Weihua Guan <[email protected]>
Stata Corp.
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/