String or not to String on Stata: Collection of usefull tools
Stata ENGRandom string utilities that saved me in the past.
Parsing cmdline after a estimation command.
. qui reg price rep78 length foreign , robust
. local what_I_typed = e(cmdline)
. di "`what_I_typed'"
regress price rep78 length foreign , robust
gettoken: be carefull with the spaces (!!)
we can split the command (regress) from the rest.
. gettoken command rhs : what_I_typed ,parse(" ,") // first token and the rest
. di "`command'"
regress
. di "`rhs'"
price rep78 length foreign , robust
Also we can split the explained variable (price) from all the rest.
. gettoken y x_plus_options : rhs ,parse(" ,") // first token and the rest
. di "`x_plus_options'"
rep78 length foreign , robust
Finally we can parse the regressors from the options using parse(“,”) instead of parse(" ,"). Yes, they are different.
. gettoken x options : rhs ,parse(",") // split using "," (look carefully!)
. di "`x'"
price rep78 length foreign
. di "`options'"
, robust
word() function
Get the first and last regressors
. loc first_x `=word("`x'",1)'
. di "`first_x'"
rep78
. loc last_x `=word("`x'",-1)'
. di "`last_x'"
foreign