Announcement

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

  • renaming using loop

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str43 Q2_8a1 double(Q2_8b1 Q2_8c1 Q2_8d1 Q2_8e1) str43 Q2_8a2    double(Q2_8b2    Q2_8c2    Q2_8d2    Q2_8e2)    str43    Q2_8a3    double(Q2_8b3    Q2_8c3    Q2_8d3    Q2_8e3)
    "anil mongar"        24 1 10 1 "" . . . . "" . . . .
    "Nir mala"           34 2 21 1 "" . . . . "" . . . .
    "Suk mith lepcha"    64 2 21 1 "" . . . . "" . . . .
    "pemba lepcha"       50 1  3 1 "" . . . . "" . . . .
    "sangay tenzin"      77 1  8 1 "" . . . . "" . . . .
    "Nar Badhur"         32 1  7 1 "" . . . . "" . . . .
    "Pema Wangmo Lepcha" 29 2  5 1 "" . . . . "" . . . .
    "neelam"             40 2 12 2 "" . . . . "" . . . .
    "tandin bida"        43 2 10 1 "" . . . . "" . . . .
    ""                    . .  . . "" . . . . "" . . . .
    "Durga"              31 1  8 1 "" . . . . "" . . . .
    "nim dem"            23 2 12 1 "" . . . . "" . . . .
    ""                    . .  . . "" . . . . "" . . . .
    "Namgay Dem"         32 2  8 1 "" . . . . "" . . . .
    "kumari tamang"      41 1 21 1 "" . . . . "" . . . .
    "Phuntsho wangdi"    48 1  9 1 "" . . . . "" . . . .
    "Chungku"            27 2  2 1 "" . . . . "" . . . .
    "Deoraj Gurung"      43 1 17 1 "" . . . . "" . . . .
    ""                    . .  . . "" . . . . "" . . . .
    "ganga maya ghally"  45 2 21 1 "" . . . . "" . . . .
    end
    label values Q2_8c1 Q2_8c
    label values Q2_8c2 Q2_8c
    label values Q2_8c3 Q2_8c
    label def Q2_8c 1 "Male", modify
    label def Q2_8c 2 "Female", modify
    label values Q2_8d1 Q2_8d
    label values Q2_8d2 Q2_8d
    label values Q2_8d3 Q2_8d
    label def Q2_8d 2 "Grade 2", modify
    label def Q2_8d 3 "Grade 3", modify
    label def Q2_8d 5 "Grade 5", modify
    label def Q2_8d 7 "Grade 7", modify
    label def Q2_8d 8 "Grade 8", modify
    label def Q2_8d 9 "Grade 9", modify
    label def Q2_8d 10 "Grade 10", modify
    label def Q2_8d 12 "Grade 12", modify
    label def Q2_8d 17 "Bachelor's degree", modify
    label def Q2_8d 21 "No education", modify
    label values Q2_8e1 Q2_8e
    label values Q2_8e2 Q2_8e
    label values Q2_8e3 Q2_8e
    label def Q2_8e 1 "Bhutanese", modify
    label def Q2_8e 2 "Indian", modify
    Dear All
    How can i use loop to rename all variables from Q2_8a as name1,Q2_8b as age1 Q8_8c as sex1 Q8_8d as edu_level1 and Q8_8e nationaliy1 ajnd simultaneously name2, age2, sex2, edu2, nationality2

  • #2
    No loop needed:

    Code:
    rename Q2_8a* name*
    rename Q2_8b* age*
    rename Q2_8c* sex*
    rename Q2_8d* edu_level*
    rename Q2_8e* nationality*

    Comment


    • #3
      even for the label will the same command work. Can you also tell me how to do in loop

      Comment


      • #4
        even for the label will the same command work
        No, -rename- only changes the variable names, not the labels. Actually, in the data example you showed, the variables don't have any labels, so I don't even understand what you might want to do.

        even for the label will the same command work
        Yes, I could, but why would you want to do that? It doesn't make any sense to me.

        Comment


        • #5
          I wanted to use that for other variables. If you caould use this example and tell me then i could apply it on others. Can you also tell me a shorter way to label the variables as well , the labels would be age of the person, name of the person, education level of the person etc

          Comment


          • #6
            I still don't understand why you want to do this. Systematic renaming of variables is best done with the -rename- command as shown. But since you insist, here's a harder, and slower, way to do it with loops.

            Code:
            local targets name age sex edu_level nationality
            local sources Q2_8a Q2_8b Q2_8c Q2_8d Q2_8e
            local n_targets: word count `targets'
            forvalues i = 1/`n_targets' {
                local s: word `i' of `sources'
                local t: word `i' of `targets'
                foreach v of varlist `s'* {
                    local newname: subinstr local v "`s'" "`t'"
                    rename `v' `newname'
                    label var `newname' `"`newname'"'
                }
            }

            Comment


            • #7
              Originally posted by Gliby Anna View Post
              Can you also tell me a shorter way to label the variables as well , the labels would be age of the person, name of the person, education level of the person etc
              You can use elabel (SSC) for that. Building on Clyde's code in #2 and #6

              Code:
              local a name
              local b age
              local c sex
              local d edu_level
              local e nationality
              
              foreach x in a b c d e {
                  // rename variables
                  rename Q2_8`x'* ``x''*
                  // label variables
                  elabel variable (``x''*) ("``x'' of person")
              }
              
              // change value label names
              elabel rename (Q2_8c Q2_8d Q2_8e) (sex edu_level nationality)
              Best
              Daniel

              Last edited by daniel klein; 28 Feb 2019, 23:59.

              Comment


              • #8
                Thank you so much

                Comment

                Working...
                X