Announcement

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

  • How to label variables in a cycle way

    Dear Stata users,

    I have a dataset with 170+ variables, and these variables indicate same information for different series. For example, variable mpg1 indicates mileage for car type A, and mpg2 indicates mileage for car type B....... However in my real dataset mpg1 is named AZ, mpg2 is named BE. My question is how can I label these variables in a cycle way? That is to say, label variable mpg1 mpg2 mpg3 mpg4 mpg5 (but named AZ BE BJ BO BT in practice) as "Mileage (mpg)". Thank you.

    The following example illustrate the data structure preliminarily. And what's more, in my practical dataset, variables are named in alphabetic, i.e. A, B, C, D, E, ......, FK, FL, FM, FN, FO. So I can't label variables according to their name pattern, for example label variable mpg`n' "Mileage (mpg)" in a loop.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str14 make1 int price1 byte mpg1 float headroom1 str17 make2 int price2 byte mpg2 float headroom2 str17 make3 int price3 byte mpg3 float headroom3 str14 make4 int price4 byte mpg4 float headroom4 str14 make5 int price5 byte mpg5 float headroom5
    ""                  .  .   . ""                      .  .   . "AMC Concord"        4099 22 2.5 ""                  .  .   . ""                   .  .   .
    ""                  .  .   . ""                      .  .   . "AMC Pacer"          4749 17   3 ""                  .  .   . ""                   .  .   .
    ""                  .  .   . ""                      .  .   . ""                      .  .   . ""                  .  .   . ""                   .  .   .
    ""                  .  .   . ""                      .  .   . "Buick Century"      4816 20 4.5 ""                  .  .   . ""                   .  .   .
    ""                  .  .   . ""                      .  .   . ""                      .  .   . "Buick Electra"  7827 15   4 ""                   .  .   .
    ""                  .  .   . ""                      .  .   . "Buick LeSabre"      5788 18   4 ""                  .  .   . ""                   .  .   .
    ""                  .  .   . ""                      .  .   . ""                      .  .   . ""                  .  .   . ""                   .  .   .
    ""                  .  .   . ""                      .  .   . "Buick Regal"        5189 20   2 ""                  .  .   . ""                   .  .   .
    ""                  .  .   . ""                      .  .   . "Buick Riviera"     10372 16 3.5 ""                  .  .   . ""                   .  .   .
    ""                  .  .   . ""                      .  .   . "Buick Skylark"      4082 19 3.5 ""                  .  .   . ""                   .  .   .
    end

  • #2
    Code:
    rename (A-FO) mpg#, addnumber 
    
    foreach v of var mpg* { 
         label var `v' "Mileage (mpg)" 
    }
    except that in practice giving numerous variables exactly the same variable label will confuse mightily and clarify little!

    Comment


    • #3
      Sorry Nick, I didn't express my question clearly. There are not only mpg# variables, but also let's say make#, price#, headroom#……In fact every five variables constitute a serie. So,
      AZ BA BB BC BD are correspond to (only for example) make1 price1 mpg1 headroom1 weight1
      BE BF BG BH BI are correspond to make2 price2 mpg2 headroom2 weight2
      BJ BK BL BM BN are correspond to make3 price3 mpg3 headroom3 weight3
      , and so on. There are 170+ variables that follow this pattern.
      I want to label BA, BF, BK…… as Price, label BB, BG, BL…… as Mileage (mpg).

      Comment


      • #4
        Indeed. I didn't look closely enough at your question. Let's suppose there are 175 such variables. Then divide by 5 to get 35.

        You need

        Code:
        local new 
        forval j = 1/35 { 
               local new `new' make`j' price`j' mpg`j' headroom`j' weight`j' 
        }
        and then that is the harder part of the rename command done. You can add code for variable labels to the loop.

        Comment


        • #5
          Thank you so much Nick Cox. Magic and perfect code! I'm not good at using local, and I have to resort to Jeroen Weesie and your -renvars- command (sj5-4) to complete my task.

          Code:
          drop A
          local new 
          forval j = 1/34 { 
              local new `new' make`j' price`j' mpg`j' headroom`j' weight`j' 
          }
          
          renvars B-FO \ `new'
          
          forval j=1/34 {
              label variable make`j' "Make and Model"
              label variable price`j' "Price"
              label variable mpg`j' "Mileage (mpg)"
              label variable headroom`j' "Headroom (in.)"
              label variable weight`j' "Weight (lbs.)"
           }

          Comment


          • #6
            renvars (last revised in the Stata Journal 2005) belongs at most in the history books.

            For some years it was the only systematic way to rename lots of variables unless you wrote your own loops. StataCorp kept promising to do better, and in due course redeemed their promise spectacularly with the new rename, which suitably ignored renvars completely. There are a few things you could do with renvars that are hard with rename, but many more things the other way round.

            I stopped using renvars myself years and years ago. I often have to look up the syntax I need with rename, but that's a small price to pay.

            Comment


            • #7
              Thank you Nick, I can get there using code like
              Code:
              rename (B-FO) (`new')
              But when I find a series of user-written commands like -labutil- (Nick) -labutil2- (Daniel Klein) -renvars- -labvars- -labdeval- -labvarch-, and so on, I use them quite so often.

              Comment


              • #8
                In the early days of StataCorp there was quite strong emphasis on folding good community-contributed commands back into official Stata. Now not so much, as (1) there are so many of them that someone might deem good for some purpose (2) firewall problems for some aside, most users can install what they want easily and if it's any good it works seamlessly (3) it's a better use of StataCorp developers' time to work on really big projects that no user is likely to implement or replicate (4) whatever StataCorp implements, it needs to support indefinitely.

                Comment

                Working...
                X