I've recently had occasion to write a program using the colon operator, where as is typical, an estimation command comes after the colon. I'm seeking advice about a less clumsy way to put an "if touse" command in place of any if/in, so as to be able to modify the touse. The context is that I want to walk through the sample, repeatedly applying the estimation command to the dataset and dropping one case at a time so as to implement a brute-force analysis of influential observations for commands for which dfbeta and so forth don't apply.)

Here's my clumsy way to extract the estimation command, chop off any options on it, process the first part of that command so as to replace any if/in with a "if touse, " and put the options back on the estimation command. The way I've done this works, but seems clumsy, and I think I'd benefit more generally from getting advice about intelligent parsing of syntax. Here's what I have done
Code:
// My program has a syntax like this-- MyProg, option1 option2: EstimationCommand
// Extract and work on the user's estimation command
capture _on_colon_parse `0'
local estimcmd = `"`s(after)'
// Get part of estimation command before options start at comma
local pos = strpos("`estimcmd'", ",")
if ((`pos') > 0 ) {    // options exist on the estimation command, so put them aside for the moment
   local commaafter= substr("`estimcmd'", `pos', .)
   local estimcmd = substr( "`estimcmd'", 1, `pos'-1)  // just the command
}
// Make a touse variable
local 0 `estimcmd'
syntax anything [if] [in]
marksample touse  // record existing if/in
//
// Replace if/in material in estimation command string with an "if touse" clause.  
local estimcmd = subinstr("`estimcmd'", "`in'", "", .)  // strip out in
local estimcmd = subinstr("`estimcmd'", "`if'", "", .)  // strip out "if" material
local estimcmd = "`estimcmd'" + " if " + "\`touse'"  +  "`commaafter'"  // touse and options onto the estimation command
Having done the preceding, I can then easily and efficiently loop through the sample, modifying the `touse' variable at each iteration so as to drop each observation in turn while executing the estimation command. Besides better parsing approaches, I'd also be interested in other strategies for the larger problem.

I understand that there are syntax features for capturing options and so forth, but the documentation is distinctly thin there. The maneuvers I see in the code for the built-in colon commands are much fancier than I'm ready to handle at this point.

Thanks for taking a look at this.