Announcement

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

  • Rename variable with its own label

    Dear all,

    I am struggling with the following problem:
    I have several datasets I want to append, but the variable names differ from one dataset to the next.
    My variables are called very uniformative v1 to v300 and the information is caught in the lables, e.g. v1 ~ "Interviewdate". I would like to rename all variables by its label, but do not find the right command.

    I have found the following loop, but I am not sure how to adapt the variable names:

    foreach v of varlist _all{
    local x= variable label `v'
    rename `v' `x'
    }

    , but STATA tells me "variable not found". If I drop the word "variable", it tells me "label not found".

    Could anybody tell me how to define it better, that it works for my case?

    I would be really happy!

    Best
    Nadine

  • #2
    Nadine,

    Welcome to Statalist.

    I found the following after a quick Internet search. (Sometimes you just need to look for the right combination of words to search on. I searched on "stata store variable label in local.")

    http://www.stata.com/statalist/archi.../msg00087.html

    I think the question is similar to one that you have. Using the example at that link, loop through all the variables in the dataset and then use -rename- instead of -replace-.

    Best,
    Lance
    Last edited by Lance Erickson; 10 Dec 2016, 10:13. Reason: I originally said the question was the same but it was only similar.

    Comment


    • #3
      Welcome to Statalist!

      I think your loop would work with just one change. Reading the output of help macro we see that to use the macro "extended functions" (which "variable label" is) we have to replace the equal sign "=" with a colon ":" so the following should get you closer to what you want;
      Code:
      foreach v of varlist _all {
      local x : variable label `v'
      rename `v' `x'
      }
      With that said, this relies on the variable labels all being suitable for use as variable names - no embedded spaces, no special characters, etc.

      Comment


      • #4
        Thank you for your quick responses!

        I have changed the "=" for an ":" and read the attached article, but still get the same error message:

        syntax error
        Syntax is
        rename oldname newname [, renumber[(#)] addnumber[(#)] sort ...]
        rename (oldnames) (newnames) [, renumber[(#)] addnumber[(#)] sort ...]
        rename oldnames , {upper|lower|proper}

        . I think I do better understand now how the loop should generally work, but I can't understand the message in this case.

        I use this code:
        foreach v of varlist _all { local x: variable label `v' rename `v' `x' } in the third line it seems to work that all variables are caught by using `v', but in the rename command it does not run the loop. Do you have any idea why this happens? Thank you again!

        Comment


        • #5
          William already answered this:

          With that said, this relies on the variable labels all being suitable for use as variable names - no embedded spaces, no special characters, etc.
          That's with most datasets very unlikely.

          Study this example

          Code:
          sysuse auto, clear
          
          foreach v of var * {
              local lbl : var label `v'
              local lbl = strtoname("`lbl'")
              rename `v' `lbl'
          }
          
          describe

          Comment


          • #6
            Perfect, thank you! Thanks to the hint to include this third row "local lbl = ..." it finally works :-)

            Comment


            • #7
              Dear Prof. Nick,

              Might I skip the error if I met "variable XXXX already defined " when running your code? Thank you very much.

              Comment


              • #8
                Code:
                  
                 capture rename `v' `lbl'

                Comment

                Working...
                X