Announcement

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

  • renaming all value labels in a dataset

    I currently am working to merge two data sets. They both have unique variable names except FamilyID which I'm using to match the data on. My two data sets are a parents data set and child data set that I wish to merge. I have successfully merged them but the value labels are wrong for the merged data set because the value label names are the same between the two data sets, but they indicate different values between them. For example, in parents data set the value label "labels8" hold information for race 1 "Black" 2 "Hispanic" etc and "labels8" in the child data is gender identity 1 "Male" 2 "Female" etc. when I merge the two data sets the value label for the child takes on the value label for the parents "labels8" since it holds the same name.

    I would like to rename all my value labels in one of my data sets to add a prefix or suffix so that I do not have this problem when I merge. Incidentally all my value labels begins with "labels" followed by a number. I also have 516 value labels so I can't just go change them all easily.

    Any help would be appreciated. This is my first post and I'm a newbie to STATA.
    Thank you!

  • #2
    Code:
    use dataset2, clear
    elabel rename (*) (*_2)
    will add the suffix _2 to all of the labels in dataset2.

    The -elabel- package is by Daniel Klein and is available from SSC.

    Comment


    • #3
      I am currently stuck at a similar point, just i want to add a suffix to the already existing variable labels. This command only adds the suffix to the value label.

      I do not want to add a suffix to value labels, but to variable labels. Can anyone help?
      Last edited by Svenja Pawlik; 15 Jul 2024, 08:37.

      Comment


      • #4
        Code:
        foreach var of varlist *{
            cap label variable `var' "`:var lab `var''_2"
        }
        Last edited by Andrew Musau; 15 Jul 2024, 08:52.

        Comment


        • #5
          Something like this?

          Code:
          local vars var1 var2 var3
          foreach var of local vars {
          local varname : variable label `var' // get variable label for variable `var' label variable `var' "‎prefix_`varname'_suffix" // modify but still include `varname'
          }

          Comment


          • #6
            Originally posted by Svenja Pawlik View Post
            This command [elabel] only adds the suffix to the value label.
            No. The (extensive) help file points to

            Code:
            elabel variable (*) (= @+"_2")

            Comment


            • #7
              This one is easy to do:
              Code:
              foreach v of varlist _all {
                  local lbl: var label `v'
                  label var `v' `"`lbl'-2"'
              }
              Added: Crossed with #6.

              Comment

              Working...
              X