Announcement

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

  • Label variables using loop

    Hello stata community,

    first post here! i have q14_1 q14_2 and q14_3 as variable name and its label is Skill/Majors 1, Skill/Majors 2, Skill/Majors 3
    I;d like to change variables' label by using shorter command instead of:

    label var q14_1 "Skill/Majors 1"
    label var q14_2 "Skill/Majors 2"
    label var q14_3 "Skill/Majors 3"

    Is there any short command like loop?

    Thank in advance! that would save lots of time with my cleaning process.

  • #2
    I recommend a user-written command -labvars-. daniel klein is its author, and it is a part of the integrated program named -labutil2- now. You can search it in Stata command window and install, or just type ssc install labvars to install. As to your case, you can try the following code:
    Code:
    labvars q14_1 q14_2 q14_3 \ "Skill/Majors 1" "Skill/Majors 2" "Skill/Majors 3"

    Comment


    • #3
      Code:
      forval j = 1/3 { 
           label var q14_`j' "Skill/Majors `j'"
      }

      Comment


      • #4
        Thanks for mentioning labvars (SSC). I consider labvars superseded by elabel (SSC, SJ, GitHub); the corresponding syntax is either

        Code:
        elabel variable (q14_1 q14_2 q14_3) ("Skill/Majors 1" "Skill/Majors 2" "Skill/Majors 3")
        or

        Code:
        elabel variable q14_1 "Skill/Majors 1" q14_2 "Skill/Majors 2" q14_3 "Skill/Majors 3"
        neither of which I find convenient for this particular problem. I would go with the suggested loop in #3 here.

        Comment


        • #5
          Thank you Nick Cox for your help! it works perfectly. You're a life savior.
          Thank you Chen Samulsion and daniel klein that's another way of doing it. <3

          Comment

          Working...
          X