Announcement

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

  • Renaming long list of variables with loop

    Hello,

    I am currently working with a data set with the unit of observation as households. There are also variables for each member of the household e.g hv101_01 for person one in the household, hv101_02 for the second person in the household and up to hv101_39, all of which contain the same label. This is is the case for many variables.

    I will like to change the names of all variables to the label name. I was able to figure this out as:

    foreach v of var * {
    local lbl : var label `v'
    local lbl = strtoname("`lbl'")
    rename `v' `lbl'
    label variable `lbl' "`v'"
    }


    But when it reaches the variables that are for the second member of the household e.g hv101_02, Stata says that the variable name is already defined. I know that this is because hv101_01 already has that variable name.

    I will like to add _02 or whatever number is behind the variable when the variable is changed to the label name.
    Can someone please help with a code for this.

    Thank you for the anticipated response.
    Last edited by Oyinkansola Osunkoya; 13 Mar 2020, 16:21.

  • #2
    Do all variables end in an underscore followed by 2 digits ("_##")? If so,

    Code:
    foreach v of var * {
          local lbl : var label `v'
          local end=substr("`v'", -1, 2)
          rename `v' `lbl'_`end'
          label variable `lbl'_`end' `v'
    }

    Comment


    • #3
      Cross-posted at https://stackoverflow.com/questions/...-loop-in-stata

      Please note our policy on cross-posting, which is that you are asked to tell us about it.

      Comment


      • #4
        Sorry, I did not know about that. Do I need to do anything else?

        Comment


        • #5
          I do not get the desired result when I try out your code.

          In the loop below, all the variables in the list hvidx_01 - hv124_39 all have an underscore followed by 2 digits ("_##")

          foreach v of var hvidx_01 - hv124_39 {
          local lbl : var label `v'
          local end=substr("`v'", -1, 2)
          rename `v' `lbl'_`end'
          label variable `lbl'_`end' `v'
          }


          I included a picture of the error that I get:
          Click image for larger version

Name:	error syntax.JPG
Views:	1
Size:	43.8 KB
ID:	1541277

          Comment


          • #6
            You probably have spaces in your variable labels. Here is a fix, but there are length limits to Stata variable names, so you should start thinking about how sound your approach is.

            Code:
            foreach v of var hvidx_01 - hv124_39 {
                     local lbl : var label `v'
                     local lbl= subinstr("`lbl'"," ","_",.)
                     local end=substr("`v'", -1, 2)
                     rename `v' `lbl'_`end'
                     label variable `lbl'_`end' `v'
             }

            Comment


            • #7
              Thank you so much it now works. Yes I will take the length limits of Stata variable names into account and adjust where needed.

              Comment

              Working...
              X