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
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.
0 Response to Parsing a user-given command to add an "if touse"
Post a Comment