I have a panel data set with country, years, and the following three (relevant) variables:

C (float)
code (string)
varcode (string)

code has string values such as "a", "b", "c", etc.
varcode has string values such as "i_it_a", "i_it_b", "i_it_c", etc.

I am trying to create a new variable, i_gfcf_<code>, for each value of code, which sums over the values of C for certain values of varcode. See the loop below:

local title code
foreach x of varlist code{
egen i_gfcf_`x' = sum(C) if (varcode == "i_it_`x'") | (varcode == "i_ct_`x'") | (varcode == "i_soft_`x'") | (varcode == "i_traeq_`x'") | (varcode == "i_omach_`x'") | ///
(varcode == "i_ocon_`x'") | (varcode == "i_rstruc_`x'") | (varcode == "i_other_`x'")
}

PROBLEM: After running this loop, I obtain only one new variable, i_gfcf_code. But code is a variable name, not a value of the variable, i.e. I would expect to have only i_gfcf_a, i_gfcf_b, i_gfcf_c, etc. What's more, i_gfcf_code is incorrectly missing for all observations.

QUESTION: How can I structure the above loop such that each value of the variable code is used to
(i) generate a new variable named after that particular value of code, e.g. i_gfcf_a, i_gfcf_b, etc. and
(ii) adjust the if statement for each value of code, e.g. if (varcode == "i_it_a") | (varcode == "i_ct_a"), etc. or if (varcode == "i_it_b") | (varcode == "i_ct_b"), etc. ?

Thank you so much.