Announcement

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

  • Renaming variables with a for loop

    Suppose I have 8 variables named: DY, DZ, EA, EB, EC, ED, EE, EF.

    I want them to be renamed to the followings:

    Apple_total_harvest
    Apple_price
    Orange_total_harvest
    Orange_price
    Peach_total_harvest
    Peach_price
    Mango_total_harvest
    Mango_price

    In my actual dataset, I have a lot more variables to rename in the above manner. Therefore, I am wondering if I could write a for loop to accomplish the renaming task quickly and efficiently. I am thinking that I might have to create a "list" or something with the parts which are common to each variable name i.e. total harvest and price and create another separate list with fruits' names. Then somehow link the fruits' names to parts in the first list. But I don't know how I can do this. Any help will be highly appreciated.
    Last edited by Taz Raihan; 20 Sep 2018, 14:57.

  • #2
    Code:
    //    GENERATE TOY DATA SET TO DEMONSTRATE CODE
    clear*
    set obs 50
    set seed 1234
    forvalues i = 1/8 {
        gen var`i' = runiform()
    }
    des
    
    //    IN YOUR REAL DATA, START HERE
    local fruits Apple Orange Peach Mango
    local attributes total_harvest price
    
    local i = 1
    foreach f of local fruits {
        foreach a of local attributes {
            rename var`i' `f'_`a'
            local ++i
        }
    }

    Comment


    • #3
      Clyde Schechter Thanks a lot for your help. However, I apologize for editing my original post later to resemble my original data more closely. I changed the original variable names from var1, var2 etc... to DY, DZ...EF. Would you please adjust your code taking my edit into account? Again, I apologize for the late edit.

      Comment


      • #4
        The following makes some minor adjustments to Clyde's solution with your new variable names. This assumes that the variables are ordered sequentially in your dataset (DY is the variable located immediately before DZ; followed immediately by EA, EB, EC, etc):

        Code:
        //    GENERATE TOY DATA SET TO DEMONSTRATE CODE
        clear
        input float(DY DZ EA EB EC ED EE EF)
        1 2 3 4 5 6 7 8
        end
        
        //    IN YOUR REAL DATA, START HERE
        local fruits Apple Orange Peach Mango
        local attributes total_harvest price
        
        
        
        unab vlist: DY-EF
        
        local i = 1
        foreach f of local fruits {
            foreach a of local attributes {
                local var: word `i' of `vlist'
                noi di as result "renaming `var' -->  `f'_`a'"
                rename `var' `f'_`a'
                local ++i
            }
        }
        Stata/MP 14.1 (64-bit x86-64)
        Revision 19 May 2016
        Win 8.1

        Comment


        • #5
          Taz Raihan No problem. I see Carole J. Wilson has responded, and her solution is exactly what I would have given you had I gotten here first.

          Lesson about using this Forum, for anyone reading: when posting a question asking for code, it is important that you show data examples and that the data examples be just like your actual data. For data management, in particular, the best solution often relies on the specific names of the variables, or the storage types, or particular value labels, or the particular values the data takes on. So it is important to phrase your question, from the start, in terms that reflect the real data you want to apply the code to. Otherwise you can end up with a solution that's just fine for the imaginary data, but won't work in the real data. So resist the temptation to "simplify" the question by changing or omitting some "minor details." In coding, there are no minor details.

          Comment


          • #6
            A slightly different way.
            Code:
            local fruits Apple Orange Peach Mango
            local attributes total_harvest price
            local newnames
            
            foreach f in `fruits' {
               foreach a in `attributes' {
               local newnames "`newnames' `f'_`a'"
               }
            }
            
            ren (DY-EF) (`newnames')
            Last edited by Romalpa Akzo; 20 Sep 2018, 21:31.

            Comment


            • #7
              Someone should underline that -- for the problem stated --

              Code:
              rename (DY-EF) (Apple_total_harvest Apple_price Orange_total_harvest Orange_price Peach_total_harvest Peach_price Mango_total_harvest Mango_price)
              is a direct solution.

              Naturally, the loop solutions are a good idea for whatever the wider problem is.
              Last edited by Nick Cox; 21 Sep 2018, 05:09.

              Comment


              • #8
                Thank you, everyone. These codes are proving very useful for my work.

                Comment

                Working...
                X