Announcement

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

  • Changing many variable labels at once

    Hello,

    This is my first post as a Stata novice. I hope this post is not redundant.

    I would like to change the labels of many variables at once. Specifically, I am looking to remove the identical prefix all the labels.
    The analogue (for variable names) to what I am trying to do is renpfix.
    Naturally, I have been looking to use a loop but I can only add "prefix"--not remove the existing prefix.

    Code:
    foreach v of var * {
    local new : var label `v'
    label var `v' "prefix: `new"
    }
    I would greatly appreciate any hint as to how to achieve this goal.
    Thank you in advance.

  • #2
    Here's an approach that might help.
    Code:
    // create sample data with prefixed variable labels
    sysuse auto, clear
    foreach v of var * {
    local new : var label `v'
    label var `v' "prefix: `new'"
    }
    describe
    // remove the prefix
    foreach v of var * {
    local new : var label `v'
    local newer : subinstr local new "prefix: " "" 
    label var `v' "`newer'"
    }
    describe

    Comment


    • #3
      See labvarch (part of labutil package) from the SSC.

      Best
      Daniel

      Comment


      • #4
        @William Lisowski:Thank you very much for your reply! This is the answer I was looking for--it works!

        Comment


        • #5
          Hi, Hans - I'll echo Daniel and recommend his nice labmm ado, which is available on the ssc archive.
          Nathan E. Fosse, PhD
          [email protected]

          Comment

          Working...
          X