Announcement

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

  • Modifying a List of Variable Labels Based on Pattern

    Hi all,
    I received a dataset where most of the variable labels include something like this: "<b>something<b>...the rest of the label" where the something changes for each variable. Is there a way to remove the underlined part of label and leave the remaining portion? I know I could use subinstr to remove the "<b>"s but I'm not sure how to deal with the changing something in each label.

    Thank you,
    Daniel

  • #2
    Consider this code:

    Code:
    sysuse auto, clear
    
    label var length "Length<b>something<b> (in.)"
    label var price "Price in dollars <b>anything<b>"
    label var make "Make and <b>again just anything<b>model"
    
    foreach thisvar of varlist length price make {
        local lab: var label `thisvar'
        local newlab = ustrregexrf("`lab'", "<b>.+<b>", "")
        label var `thisvar' `"`newlab'"'
    }
    
    d length price make
    which produces:

    Code:
    Variable      Storage   Display    Value
        name         type    format    label      Variable label
    ------------------------------------------------------------------------------------------------------------------
    length          int     %8.0g                 Length (in.)
    price           int     %8.0gc                Price in dollars
    make            str18   %-18s                 Make and model

    Comment


    • #3
      Thanks so much Hemanshu! This worked great.

      Comment

      Working...
      X