Announcement

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

  • Putting labels for different variables in one line of code or loop?

    Hello,

    I would like to add labels for a set of different variables, i have around 15, and would like to know if its possible to do it in one line (for example, when you encode a variable, you can then, label the variables in the same line.

    However, I have a few dummies already as variables and would like to know if its possible to label them in one line of code.

    Thanks!

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(ba_street_change ba_city_change ba_zip_change ma_street_change)
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    1 1 1 1
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    end

  • #2
    See -help label values-. It takes a varlist, so you can simply specify label values ba_* labelname.

    Comment


    • #3
      Hi Falco,

      Hope this help.

      Code:
      local name "name1 name2 name3 name4" // as you have 4 variables 
      local j=0
      foreach x of varlist _all {
         local j=`j'+1
         local label: word `j' of `name'
         label var `x' "`label'"
         }

      Comment


      • #4
        Originally posted by Jesse Wursten View Post
        See -help label values-. It takes a varlist, so you can simply specify label values ba_* labelname.
        Jesse's suggestion is for value label. My code is for label and it's a loop but not 1 line of code

        Comment


        • #5
          Leon Thanks.

          Lets say I wanted to do it for a local varlist, and with labels "x" "y" "c" "b", would it be possible?

          Comment


          • #6
            If you want variable labels (and for much more), see elabel (from SSC). The loop in #3 is implemented so this is a one-liner

            Code:
            elabel variable (`varlist') ("x" "y" "c" "b")
            Best
            Daniel

            Comment


            • #7
              Daniel,

              I tried doing:
              elabel variable (`streetchange citychange state change zipchange') ("Business Address Street Change" "Business Address City Change" "Business Address State Change" "Business Address ZIP change" ).

              But it gives me back: () invalid name

              Do you know what the problem is?

              Comment


              • #8
                I see that you are probably new to Stata so terminology is a bit of a problem here. When you said you

                wanted to do it for a local varlist
                I assumed that you have your variable list stored in a local (macro) named varlist. Therfore, I have suggested expanding that local macro inside the first pair of parentheses. It now turns out that you actually want to list the variable names. In that case, just delete the single quotes and type

                Code:
                elabel variable (streetchange citychange state change zipchange) ("Business Address Street Change" "Business Address City Change" "Business Address State Change" "Business Address ZIP change")
                This will still give you an error because you list 5 variables but only 4 labels. Perhaps you meant to write statechange not state change.

                Sometimes, it is better to write more code to make it more readable. Consider

                Code:
                label variable streetchange "Business Address Street Change"
                label variable citychange   "Business Address City Change"
                label variable statechange  "Business Address State Change"
                label variable zipchange    "Business Address ZIP change"
                If you are getting tired of tying label variable, elabel offers this variation

                Code:
                elabel variable                                   ///
                    streetchange "Business Address Street Change" ///
                    citychange   "Business Address City Change"   ///
                    statechange  "Business Address State Change"  ///
                    zipchange    "Business Address ZIP change"
                Because the labels can be inferred from variable names, you could write a clever loop. Whether that loop is so clever in terms of understanding what is going on is another question.

                [Edit]

                Alright, for completeness

                Code:
                foreach x in street city state zip {
                    label variable `x'change `"Business Address `=strproper("`x'")' Change"'
                }
                That will produce Zip in the label, not ZIP; you would need to fix that.

                Code:
                elabel variable (zipchange) (= subinstr(@, "Zip", "ZIP", .))
                [/Edit]

                Best
                Daniel
                Last edited by daniel klein; 25 Jan 2019, 05:16.

                Comment


                • #9
                  Thanks Daniel. I understand now.

                  Comment


                  • #10
                    Hi Falco,

                    I think Daniel give a good way to do that.
                    about code I give you, the command
                    Code:
                    local name "name1 name2 name3 name4"
                    is for local list name you need.

                    Comment

                    Working...
                    X