Announcement

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

  • Label variable: Multiple variables at once

    Hello everyone, completely new to Stata. Is there a simple way to label multiple variables for a dataset with lets say 50 variables?

    Do I have to type the code below 50 different times with each line for a different variable like the example below?

    label variable make "Make"
    label variable price "Price"
    label variable mpg "Miles per gallon"

    I would appreciate if anyone could point me in the right direction. Thanks.



  • #2
    Do I have to type the code below 50 different times with each line for a different variable like the example below?
    You can probably reduce the number of lines of code below 50, but perhaps only by a small amount.

    Some of your labels are clearly related to the name of the variable. So you could do:
    Code:
    foreach v of varlist make price {
        label var `v' `"`=upper(`"`v'"')'"'
    }
    And you can add any other variables whose label is just the variable name in upper case to that.

    But for a variable like mpg, there is no systematic correspondence between the string "mpg" and the string "Miles per gallon." So for these situations where there is no simple function that can calculate the label from the variable name, you have no useful alternative to just -label variable mpg "Miles per gallon"-.

    Comment


    • #3
      Clyde Schechter Thank you.

      Comment


      • #4
        As the desire is for variable label Price not PRICE, the function proper() was perhaps intended in #2 by Clyde Schechter

        Comment


        • #5
          Yes, Nick has correctly pointed out that I intended -proper()-, not -upper()-.

          Comment


          • #6
            If it's any help, you could something like this.

            I created an excel file with variable name (vvvlab) and variable label (vvvlab).

            Click image for larger version

Name:	Screen Shot 01-27-24 at 01.06 PM.PNG
Views:	1
Size:	10.5 KB
ID:	1741308


            Code:
            import excel using autolab, firstrow clear
            local N = _N
            forv iii = 1/`N' {
                local vvvname = vvvname[`iii']
                local vvvlab = vvvlab[`iii']
                di "label variable " `"`vvvname' "' `""`vvvlab'""' "
            }
            This provides the code that you can copy and paste into your do file.

            Code:
            label variable make "Make and model"
            label variable price "Price"
            label variable mpg "Mileage (mpg)"
            label variable rep78 "Repair record 1978"
            label variable headroom "Headroom (in.)"
            label variable trunk "Trunk space (cu. ft.)"
            label variable weight "Weight (lbs.)"
            label variable length "Length (in.)"
            label variable turn "Turn circle (ft.)"
            label variable displacement "Displacement (cu. in.)"
            label variable gear_ratio "Gear ratio"
            label variable foreign "Car origin"
            I think frames could be used to actually do the labeling. This might be a useful command.

            Comment


            • #7
              There are few label utilities in ssc.

              labvars simplies the code a little

              Code:
              labvars make "Make and Model"

              Comment


              • #8
                As noted in #2, if there is no systematic mapping between variable names and variable labels, there is only so much typing that can be reduced. elabel (from SSC, SJ, or GutHub) lets you type something like

                Code:
                elabel variable ///
                    make "Make" ///
                    price "Price" ///
                    mpg "Miles per gallon"
                or

                Code:
                elabel variable ///
                    (make price mpg) ///
                    ("Make" "Price" "Miles per gallon")
                meaning you only have to type the elabel variable command once, then type all the labels. Whether that is worth the added dependency on a thrid-party, community-contributed software, I don't know.

                Comment

                Working...
                X