|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| program define ms_fvstrip, rclass
|
| version 11
|
| syntax [anything] [if] , [ dropomit expand onebyone NOIsily addbn]
|
|
|
| if "`expand'"~="" {
|
| if "`onebyone'"=="" {
|
|
|
| if (strpos("`anything'", ".")) {
|
| fvexpand `anything' `if'
|
| local anything `r(varlist)'
|
| }
|
| else {
|
| unab anything : `anything'
|
| }
|
| }
|
| else {
|
|
|
|
|
| while ("`anything'" != "") {
|
| gettoken vn anything : anything, bind
|
| if (strpos("`vn'", ".")) {
|
| fvexpand `vn' `if'
|
| local newlist `newlist' `r(varlist)'
|
| }
|
| else {
|
| unab vn : `vn'
|
| local newlist `newlist' `vn'
|
| }
|
| }
|
| local anything : list clean newlist
|
| }
|
| }
|
| foreach vn of local anything {
|
| if "`dropomit'"~="" {
|
| _ms_parse_parts `vn'
|
|
|
|
|
|
|
|
|
| loc omit_var = `r(omit)'
|
| loc is_omitted_base 0
|
|
|
| if (`omit_var') {
|
| if ("`r(type)'" == "factor") {
|
| loc is_omitted_base = "`r(base)'" == "1"
|
| }
|
| else if ("`r(type)'" == "interaction") {
|
| loc k = r(k_names)
|
| _assert !mi(`k')
|
| forval i = 1/`k' {
|
| if ("`r(base`i')'" == "1") loc is_omitted_base 1
|
| }
|
| }
|
| else {
|
| return list
|
| _assert 0, msg("Invalid var. type: `r(type)'")
|
| }
|
| }
|
|
|
| if (`is_omitted_base') {
|
| loc omit_var 0
|
| loc vn "@`vn'"
|
| }
|
|
|
| if !`omit_var' {
|
| local unstripped `unstripped' `vn'
|
| }
|
|
|
|
|
| }
|
| else {
|
| local unstripped `unstripped' `vn'
|
| }
|
| }
|
|
|
|
|
|
|
| foreach vn of local unstripped {
|
|
|
| if strpos("`vn'", "@") == 1 {
|
| loc svn : subinstr loc vn "@" ""
|
| local fullstripped `fullstripped' `svn'
|
| loc nobase `nobase' 0
|
| continue
|
| }
|
|
|
| local svn ""
|
| _ms_parse_parts `vn'
|
| if "`r(type)'"=="variable" & "`r(op)'"=="" {
|
| local svn `vn'
|
| }
|
| else if "`r(type)'"=="variable" & "`r(op)'"=="o" {
|
| local svn `r(name)'
|
| }
|
| else if "`r(type)'"=="variable" {
|
| local op `r(op)'
|
| local op : subinstr local op "o" "", all
|
| if ("`addbn'"!="") ExpandBN `op'
|
| local svn `op'`bn'.`r(name)'
|
| }
|
| else if "`r(type)'"=="factor" {
|
| local op `r(op)'
|
| local op : subinstr local op "b" "", all
|
| local op : subinstr local op "n" "", all
|
| local op : subinstr local op "o" "", all
|
| if ("`addbn'"!="") ExpandBN `op'
|
| local svn `op'`bn'.`r(name)'
|
| }
|
| else if "`r(type)'"=="interaction" {
|
| forvalues i=1/`=r(k_names)' {
|
| local op `r(op`i')'
|
| local op : subinstr local op "b" "", all
|
| local op : subinstr local op "n" "", all
|
| local op : subinstr local op "o" "", all
|
| if ("`addbn'"!="") ExpandBN `op'
|
| local opv `op'`bn'.`r(name`i')'
|
| if `i'==1 {
|
| local svn `opv'
|
| }
|
| else {
|
| local svn `svn'#`opv'
|
| }
|
| }
|
| }
|
| else if "`r(type)'"=="product" {
|
| di as err "ms_fvstrip error - type=product for `vn'"
|
| exit 198
|
| }
|
| else if "`r(type)'"=="error" {
|
| di as err "ms_fvstrip error - type=error for `vn'"
|
| exit 198
|
| }
|
| else {
|
| di as err "ms_fvstrip error - unknown type for `vn'"
|
| exit 198
|
| }
|
| local stripped `stripped' `svn'
|
| local fullstripped `fullstripped' `svn'
|
| loc nobase `nobase' 1
|
| }
|
|
|
| local stripped : list retokenize stripped
|
| local fullstripped : list retokenize fullstripped
|
|
|
|
|
| if "`noisily'"~="" {
|
| di as result "varlist=`stripped'"
|
| di as result "fullvarlist=`fullstripped'"
|
| di as result "nobase=`nobase'"
|
| }
|
|
|
| return local varlist `stripped'
|
| return local nobase `nobase'
|
| return local fullvarlist `fullstripped'
|
| end
|
|
|
| cap pr drop ExpandBN
|
| program ExpandBN
|
| args op
|
|
|
| if (!mi(real("`op'"))) loc bn "bn"
|
| c_local bn `bn'
|
| end
|
|
|