Hi all!

I've tried to follow the instructions from another post on this forum to concatenate variables with missing values and am still getting stuck with extra characters in my string.

This particular survey question is a multi-select list of countries - so 204 variables - and many respondents only select one or two, so I end up with lots of extra semi-colons and spaces if I use:
egen pre_entity_market_all = concat(pre_entity_market_unsure pre_entity_market_AF pre_entity_market_AL pre_entity_market_DZ pre_entity_market_AD pre_entity_market_AO pre_entity_market_AG pre_entity_market_AR pre_entity_market_AM pre_entity_market_AU pre_entity_market_AT pre_entity_market_AZ [...] pre_entity_market_ZM pre_entity_market_ZW), p("; ")
Following the instructions in the post linked above, I attempted multiple combinations of replace commands [ex. replace pre_entity_market_all = subinstr(pre_entity_market_all, ".", "", .)] but would still end up with a few additional semi-colons.

The code that seemed to get me the cleanest final concatenated value involved replacing each individual country variable string to have the necessary punctuation. I tried "United States; " but using "United States ; " (with that extra space that I removed later) seemed to work better. This looks something like...
  1. replace pre_entity_market_unsure = subinstr(pre_entity_market_unsure, "Unsure", "Unsure ; ", .)
    replace pre_entity_market_AF = subinstr(pre_entity_market_AF, "Afghanistan", "Afghanistan ; ", .)
    replace pre_entity_market_AL = subinstr(pre_entity_market_AL, "Albania", "Albania ; ", .)
    replace pre_entity_market_DZ = subinstr(pre_entity_market_DZ, "Algeria", "Algeria ; ", .)
    etc
    1. problem is I'm getting a type mismatch error here if any given variable has only missing values, which many do - so I need to only run the countries that have been selected by respondents, which is obviously not ideal
  2. egen pre_entity_market_all = concat(pre_entity_market_unsure pre_entity_market_AF pre_entity_market_AL pre_entity_market_DZ pre_entity_market_AD pre_entity_market_AO pre_entity_market_AG pre_entity_market_AR pre_entity_market_AM pre_entity_market_AU pre_entity_market_AT pre_entity_market_AZ pre_entity_market_BS pre_entity_market_BH pre_entity_market_BD pre_entity_market_BB pre_entity_market_BY pre_entity_market_BE pre_entity_market_BZ pre_entity_market_BJ pre_entity_market_BM pre_entity_market_BT pre_entity_market_BO pre_entity_market_BA pre_entity_market_BW pre_entity_market_BR pre_entity_market_BN pre_entity_market_BG pre_entity_market_BF pre_entity_market_BI pre_entity_market_KH pre_entity_market_CM pre_entity_market_CA pre_entity_market_CV pre_entity_market_CF pre_entity_market_TD pre_entity_market_CL pre_entity_market_CN pre_entity_market_CO pre_entity_market_KM pre_entity_market_CD pre_entity_market_CG pre_entity_market_CR pre_entity_market_CI pre_entity_market_HR pre_entity_market_CU pre_entity_market_CW pre_entity_market_CY pre_entity_market_CZ pre_entity_market_DK pre_entity_market_DJ pre_entity_market_DM pre_entity_market_DO pre_entity_market_TP pre_entity_market_EC pre_entity_market_EG pre_entity_market_SV pre_entity_market_GQ pre_entity_market_ER pre_entity_market_EE pre_entity_market_ET pre_entity_market_FJ pre_entity_market_FI pre_entity_market_FR pre_entity_market_GA pre_entity_market_GM pre_entity_market_GE pre_entity_market_DE pre_entity_market_GH pre_entity_market_GR pre_entity_market_GD pre_entity_market_GT pre_entity_market_GN pre_entity_market_GW pre_entity_market_GY pre_entity_market_HT pre_entity_market_VA pre_entity_market_HN pre_entity_market_HK pre_entity_market_HU pre_entity_market_IS pre_entity_market_IN pre_entity_market_ID pre_entity_market_IR pre_entity_market_IQ pre_entity_market_IE pre_entity_market_IL pre_entity_market_IT pre_entity_market_JM pre_entity_market_JP pre_entity_market_JO pre_entity_market_KZ pre_entity_market_KE pre_entity_market_KI pre_entity_market_XK pre_entity_market_KW pre_entity_market_KG pre_entity_market_LA pre_entity_market_LV pre_entity_market_LB pre_entity_market_LS pre_entity_market_LR pre_entity_market_LY pre_entity_market_LI pre_entity_market_LT pre_entity_market_LU pre_entity_market_MO pre_entity_market_MG pre_entity_market_MW pre_entity_market_MY pre_entity_market_MV pre_entity_market_ML pre_entity_market_MT pre_entity_market_MH pre_entity_market_MR pre_entity_market_MU pre_entity_market_MX pre_entity_market_FM pre_entity_market_MD pre_entity_market_MC pre_entity_market_MN pre_entity_market_ME pre_entity_market_MA pre_entity_market_MZ pre_entity_market_MM pre_entity_market_NA pre_entity_market_NR pre_entity_market_NP pre_entity_market_NL pre_entity_market_AN pre_entity_market_NZ pre_entity_market_NI pre_entity_market_NE pre_entity_market_NG pre_entity_market_KP pre_entity_market_MK pre_entity_market_NO pre_entity_market_OM pre_entity_market_PK pre_entity_market_PW pre_entity_market_PS pre_entity_market_PA pre_entity_market_PG pre_entity_market_PY pre_entity_market_PE pre_entity_market_PH pre_entity_market_PL pre_entity_market_PT pre_entity_market_QA pre_entity_market_RO pre_entity_market_RU pre_entity_market_RW pre_entity_market_KN pre_entity_market_LC pre_entity_market_VC pre_entity_market_WS pre_entity_market_SM pre_entity_market_ST pre_entity_market_SA pre_entity_market_SN pre_entity_market_RS pre_entity_market_SC pre_entity_market_SL pre_entity_market_SG pre_entity_market_SK pre_entity_market_SI pre_entity_market_SB pre_entity_market_SO pre_entity_market_ZA pre_entity_market_KR pre_entity_market_SS pre_entity_market_ES pre_entity_market_LK pre_entity_market_SD pre_entity_market_SR pre_entity_market_SZ pre_entity_market_SE pre_entity_market_CH pre_entity_market_SY pre_entity_market_TW pre_entity_market_TJ pre_entity_market_TZ pre_entity_market_TH pre_entity_market_TL pre_entity_market_TG pre_entity_market_TO pre_entity_market_TT pre_entity_market_TN pre_entity_market_TR pre_entity_market_TM pre_entity_market_TV pre_entity_market_UG pre_entity_market_UA pre_entity_market_AE pre_entity_market_UK pre_entity_market_US pre_entity_market_UY pre_entity_market_UZ pre_entity_market_VU pre_entity_market_VE pre_entity_market_VN pre_entity_market_YE pre_entity_market_ZM pre_entity_market_ZW), p("")
  3. replace pre_entity_market_all = subinstr(pre_entity_market_all, ".", "", .)
    replace pre_entity_market_all = substr(pre_entity_market_all, 1, strrpos(pre_entity_market_all," ") -1)
    replace pre_entity_market_all = subinstr(pre_entity_market_all, " ;", ";", .)
I also can't explain why, from one dataset to another, I'm sometimes left with a semi-colon at the end of the string or elsewhere it doesn't belong. I also have this problem when trying to use the same code for different sets of variables. My solution up until now has been to fiddle around with the command in step 3 until I find something that work, but I'm hoping to find a fully reproducible way of addressing this problem if one exists.

Many thanks!