Hi all,
I'm trying to automatically replace a substring in a bunch of variable labels -- i.e. we received the data from our survey partner with the string "${f1_crop}" where the name of the crop should be. (and f2, f3, and so on.)
I've tried using macval() and using the backslash \ to escape in string literals, all to no avail.
The code below should reproduce the problem: for example, what I want to do is change the label for f1_16_1 from "How much of \${f1_crop} harvested did your household sell?" to "How much of maize harvested did your household sell?"
with the trace on, the output looks like this:
which means that somehow the macval in the subinstr isn't working as expected, even though the backslash is translating fine.
I'd give up do the label changes manually, but there are just so. many. variables. and I feel like there must be a way to make it work.
Thank you all so much!
I'm trying to automatically replace a substring in a bunch of variable labels -- i.e. we received the data from our survey partner with the string "${f1_crop}" where the name of the crop should be. (and f2, f3, and so on.)
I've tried using macval() and using the backslash \ to escape in string literals, all to no avail.
The code below should reproduce the problem: for example, what I want to do is change the label for f1_16_1 from "How much of \${f1_crop} harvested did your household sell?" to "How much of maize harvested did your household sell?"
Code:
gen f1_16_1 = . label var f1_16_1 "How much of \${f1_crop} harvested did your household sell?" foreach v of varlist f1_16_1 { local temp: var label `v' local newlabel = subinstr("`macval(`temp')'", "\${f1_crop}", "maize") label var `v' "`newlabel'" }
Code:
. foreach v of varlist f1_16_1 { 2. local temp: var label `v' 3. local newlabel = subinstr("`macval(`temp')'", "\${f1_crop}", "maize") 4. label var `v' "`newlabel'" 5. } - foreach v of varlist f1_16_1 { - local temp: var label `v' = local temp: var label f1_16_1 - local newlabel = subinstr("`macval(`temp')'", "\${f1_crop}", "maize") = local newlabel = subinstr("", "${f1_crop}", "maize") invalid syntax label var `v' "`newlabel'" } r(198); end of do-file r(198);
I'd give up do the label changes manually, but there are just so. many. variables. and I feel like there must be a way to make it work.
Thank you all so much!
Comment