Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Escape $ in handling variable labels?

    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?"

    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'"
        }
    with the trace on, the output looks like this:

    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);
    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!

  • #2
    Welcome to Statalist.

    It's simpler than you think! Here's an approach that will let you get the global variable references out of your variable labels.
    Code:
    . gen f1_16_1 = .
    
    . label var f1_16_1 "How much of \${f1_crop} harvested did your household sell?"
    
    . describe f1_16_1
    
                  storage   display    value
    variable name   type    format     label      variable label
    ------------------------------------------------------------------------------------------------
    f1_16_1         float   %9.0g                 How much of ${f1_crop} harvested did your
                                                    household sell?
    
    .
    . global f1_crop maize
    
    .
    . local newlabel : var label f1_16_1
    
    . macro list _newlabel
    _newlabel:      How much of ${f1_crop} harvested did your household sell?
    
    . label var f1_16_1 `"`newlabel'"'
    
    . describe f1_16_1
    
                  storage   display    value
    variable name   type    format     label      variable label
    ------------------------------------------------------------------------------------------------
    f1_16_1         float   %9.0g                 How much of maize harvested did your household
                                                    sell?
    The key is that in the final label var command, Stata substitutes the value of the global variable $f1_crop into the label where ${f1_crop} appears.

    Comment


    • #3
      Hi William -- of course, duh that worked perfectly!

      That's a very clever solution, although in hindsight the whole global substitution is probably why the data came that way...in any case, thank you loads for taking the time!

      Comment

      Working...
      X