I'm learning how to use the optional syntax when writing simple Stata programs and I'm wondering if it's possible to hardcode if statements while at the same time passing optional [if] statements through the syntax options.
I know that a simple function can be written like:
sysuse auto
program meanprice
syntax [if]
mean price `if'
end
and then I can for example use some optional if statements like:
meanprice if price > 6000 & rep78 > 2
However, let's say I want to hardcode the price > 6000 statement and still be able to selectively choose optional if statements. The reason I want to do this is that the part I want to hardcode is very rigorous and I always want to pass these options through some nested programs that I'm writing without having to specify them each time.
I have tried using e.g.,
program meanprice_test
syntax [if]
mean price if price > 6000 `if'
end
but this does clearly not work (to my understanding because syntax is parsing text/strings?)
Is there any simple way to achieve the desired outcome using syntax and [if]? I can think of some very tedious workarounds that I'd rather avoid.
What you are defining in Stata terms is a command, not a function.
"clearly does not work" should always be explained by giving the error message, or other explicit result that indicates a problem.
That aside, consider this:
program meanprice_test
syntax [if/]
if "`if'" != "" local if "& (`if')"
mean price if price > 6000 `if'
end
. sysuse auto
(1978 Automobile Data)
. meanprice_test if foreign
Mean estimation Number of obs = 9
--------------------------------------------------------------
| Mean Std. Err. [95% Conf. Interval]
-------------+------------------------------------------------
price | 8783.667 827.6595 6875.08 10692.25
--------------------------------------------------------------
. meanprice_test
Mean estimation Number of obs = 23
--------------------------------------------------------------
| Mean Std. Err. [95% Conf. Interval]
-------------+------------------------------------------------
price | 9655.696 635.944 8336.829 10974.56
--------------------------------------------------------------
The problem with your code is not that syntax is parsing text [that's its job, always] but that the combination of two ifs requires more care. What you had would produce stuff like ... if ... if ... which is illegal.
So, if a user supplies an if qualifier (optional for the user, but syntactically not an option)
You need to get syntax to strip off the user-supplied if. How to do this is documented in help syntax.
Then you need to use & to combine the two if conditions. Parenthesising may help.
EDIT: If quoted strings are ever to be used in the user's if, then use compound double quotes in the program:
if `"`if'"' != "" local if `"& (`if')"'
GENERAL COMMENTS: While what you want is programmable, I think it's unnecessary and questionable practice:
For an audit trail of analysis, a do-file with a keep if statement near the beginning and a corresponding log file should suffice as a reproducible record of work on a subset of data.
For an audit trail, conversely, the use of highly specialised programs with data-specific constraints built into the code is easy to misunderstand or overlook, especially for others using your work, or even yourself at a later date.
Following this strategy carries the burden of writing lots of very specific programs, a poor use of time and energy, and of little use to others.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With