I tried extracting substrings from variable names to be used as part of a condition to populate another variable (modality). But it seems the code is not working, that is, nothing is being assigned to the new variable (modality). I will appreciate your advice in this regard.
These are the variable names: fac_idx fac_link fac_oth fac_pitc fac_pmtct fac_sns fac_sti fac_tb fac_vct modality oss_idx oss_link oss_mob oss_oth oss_pitc oss_pmtct oss_sns oss_vct. Each of them are indicator variables of 0 and 1.
The code is as written below.
Thanks.
These are the variable names: fac_idx fac_link fac_oth fac_pitc fac_pmtct fac_sns fac_sti fac_tb fac_vct modality oss_idx oss_link oss_mob oss_oth oss_pitc oss_pmtct oss_sns oss_vct. Each of them are indicator variables of 0 and 1.
The code is as written below.
Thanks.
Code:
capture drop modality
gen modality = "."
quietly ds fac_* oss_*
global vars `r(varlist)'
global omit fac_cost
global want : list vars - omit
foreach want of global want {
replace modality = "vct" if substr("`want'", 5, .) == "vct" & `want'== 1
replace modality = "tb" if substr("`want'", 5, .) == "tb" & `want'== 1
replace modality = "sti" if substr("`want'", 5, .) == "sti" & `want'== 1
replace modality = "sns" if substr("`want'", 5, .) == "sns" & `want'== 1
replace modality = "pmtct" if substr("`want'", 5, .) == "pmtct" & `want'== 1
replace modality = "pitc" if substr("`want'", 5, .) == "pitc" & `want'== 1
replace modality = "oth" if substr("`want'", 5, .) == "oth" & `want'== 1
replace modality = "link" if substr("`want'", 5, .) == "link" & `want'== 1
replace modality = "idx" if substr("`want'", 5, .) == "idx" & `want'== 1
replace modality = "mob" if substr("`want'", 5, .) == "mob" & `want'== 1
}

Comment