Announcement

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

  • Rename Variables using a loop

    Good morning
    My problem is the following.
    I would like to rename my variables using a loop.
    My initial situation is this:
    I have a set of variables with this name v1 v2 v3 .... v235

    I would like to rename these variables for groups, but assigning each group of variables the same dates, from 2013 to 2019.

    So as a final result I would like this:
    REVENUES 2013
    REVENUES 2014
    REVENUES 2015
    REVENUES 2016
    REVENUES 2017
    REVENUES 2018
    REVENUES 2019
    EBITDA 2013
    EBITDA 2014
    EBITDA2015
    EBITDA 2016
    EBITDA 2017
    EBITDA2018
    EBITDA2019
    ........

    as code I had thought of using this:
    local y = 2013
    foreach v of var * {
    rename (v1-v7) REVENUES`y '
    rename (v8-v14) EBITDA`y '
    .....
    ......
    local ++ y
    }

    But unfortunately it doesn't work (I'm a newbie in Stata).
    Thank you for any answers and help!

  • #2
    This seems to be what you want for the first 14 variables:

    Code:
    clear
    
    set obs 1
    
    forval j = 1/14 {
        gen var`j' = 42
    }
    
    ds
    
    * you start here
    local stubs REVENUES EBITDA
    
    forval j = 1/14 {
        if mod(`j', 7) == 1 {
             gettoken stub stubs : stubs
             local year = 2013
        }
        else local year = `year' + 1
        
        rename var`j' `stub'`year'
    }
    
    ds
    So, the implications seem to be

    1. You need to spell out the remaining stubs.

    2. The second loop should presumably be over 1/235

    3. Something else is going on as your problem statement implies blocks of 7 variables, but 235 is not a multiple of 7.
    Last edited by Nick Cox; 31 Oct 2021, 10:26.

    Comment


    • #3
      My solution is similar to Nick's. Need to put all name prefix in the list of "var".

      Code:
      local var "REVENUES EBITDA"
      local i = 0
      
      foreach str in `var' {
          forvalues j = `=`i'*7+1'(1)`=(`i'+1)*7' {
              ren v`j' `str'`=mod(`=`j'-1',7)+2013'
          }
          local ++i
      }

      Comment


      • #4
        I was interested in seeing if rename group could help here.
        Code:
         ds
        v1   v2   v3   v4   v5   v6   v7   v8   v9   v10  v11  v12  v13  v14
        
        . local var "REVENUES EBITDA"
        
        . local i = 1
        
        . foreach stub in `var' {
          2.     local j = `i'+6
          3.     rename (v`i'-v`j') (`stub'#), addnumber(2013)
          4.     local i = `i'+7
          5. }
        
        . describe *
        
        Variable      Storage   Display    Value
            name         type    format    label      Variable label
        ------------------------------------------------------------------------------------------------
        REVENUES2013    float   %9.0g                 
        REVENUES2014    float   %9.0g                 
        REVENUES2015    float   %9.0g                 
        REVENUES2016    float   %9.0g                 
        REVENUES2017    float   %9.0g                 
        REVENUES2018    float   %9.0g                 
        REVENUES2019    float   %9.0g                 
        EBITDA2013      float   %9.0g                 
        EBITDA2014      float   %9.0g                 
        EBITDA2015      float   %9.0g                 
        EBITDA2016      float   %9.0g                 
        EBITDA2017      float   %9.0g                 
        EBITDA2018      float   %9.0g                 
        EBITDA2019      float   %9.0g
        One caveat to this approach: it is required that v1 through v245 (or whatever the final variable to be renamed is) appear sequentially in the dataset, with no intervening variables.

        Comment


        • #5
          Thanks everyone for the help!

          Comment


          • #6
            Originally posted by Nick Cox View Post
            This seems to be what you want for the first 14 variables:

            Code:
            clear
            
            set obs 1
            
            forval j = 1/14 {
            gen var`j' = 42
            }
            
            ds
            
            * you start here
            local stubs REVENUES EBITDA
            
            forval j = 1/14 {
            if mod(`j', 7) == 1 {
            gettoken stub stubs : stubs
            local year = 2013
            }
            else local year = `year' + 1
            
            rename var`j' `stub'`year'
            }
            
            ds
            So, the implications seem to be

            1. You need to spell out the remaining stubs.

            2. The second loop should presumably be over 1/235

            3. Something else is going on as your problem statement implies blocks of 7 variables, but 235 is not a multiple of 7.
            Thanks for your help.
            I have another question.
            If instead I wanted to start renaming the variables from v12, what should I do?
            Here is an example.
            Initial situation (the one before)
            v1 v2 v3 .... v235
            i want to start renaming from v12, (always using 7-stamps), so:

            (v12-v18) REVENUES 2013 REVENUES 2014 REVENUES 2015 REVENUES 2016 REVENUES 2017 REVENUES 2018 REVENUES 2019
            (v19-v25) EBITDA 2013 EBITDA 2014 EBITDA2015 EBITDA 2016 EBITDA 2017 EBITDA2018 EBITDA2019

            In this case what do I do?
            Thanks again

            Comment


            • #7
              So, you really have 224 variables to rename.

              Code:
              clear
              
              set obs 1
              
              forval j = 12/235 {
                  gen var`j' = 42
              }
              
              ds 
              
              local stubs REVENUES EBITDA `c(ALPHA)' AA BB CC DD 
              
              forval j = 12/235 {
                  if mod(`j', 7) == 5 { 
                       gettoken stub stubs : stubs
                       local year = 2013 
                  } 
                  else local year = `year' + 1 
                  
                  rename var`j' `stub'`year'
              }
              
              ds 
              
              REVENUES2013  C2013         G2013         K2013         O2013         S2013         W2013         AA2013
              REVENUES2014  C2014         G2014         K2014         O2014         S2014         W2014         AA2014
              REVENUES2015  C2015         G2015         K2015         O2015         S2015         W2015         AA2015
              REVENUES2016  C2016         G2016         K2016         O2016         S2016         W2016         AA2016
              REVENUES2017  C2017         G2017         K2017         O2017         S2017         W2017         AA2017
              REVENUES2018  C2018         G2018         K2018         O2018         S2018         W2018         AA2018
              REVENUES2019  C2019         G2019         K2019         O2019         S2019         W2019         AA2019
              EBITDA2013    D2013         H2013         L2013         P2013         T2013         X2013         BB2013
              EBITDA2014    D2014         H2014         L2014         P2014         T2014         X2014         BB2014
              EBITDA2015    D2015         H2015         L2015         P2015         T2015         X2015         BB2015
              EBITDA2016    D2016         H2016         L2016         P2016         T2016         X2016         BB2016
              EBITDA2017    D2017         H2017         L2017         P2017         T2017         X2017         BB2017
              EBITDA2018    D2018         H2018         L2018         P2018         T2018         X2018         BB2018
              EBITDA2019    D2019         H2019         L2019         P2019         T2019         X2019         BB2019
              A2013         E2013         I2013         M2013         Q2013         U2013         Y2013         CC2013
              A2014         E2014         I2014         M2014         Q2014         U2014         Y2014         CC2014
              A2015         E2015         I2015         M2015         Q2015         U2015         Y2015         CC2015
              A2016         E2016         I2016         M2016         Q2016         U2016         Y2016         CC2016
              A2017         E2017         I2017         M2017         Q2017         U2017         Y2017         CC2017
              A2018         E2018         I2018         M2018         Q2018         U2018         Y2018         CC2018
              A2019         E2019         I2019         M2019         Q2019         U2019         Y2019         CC2019
              B2013         F2013         J2013         N2013         R2013         V2013         Z2013         DD2013
              B2014         F2014         J2014         N2014         R2014         V2014         Z2014         DD2014
              B2015         F2015         J2015         N2015         R2015         V2015         Z2015         DD2015
              B2016         F2016         J2016         N2016         R2016         V2016         Z2016         DD2016
              B2017         F2017         J2017         N2017         R2017         V2017         Z2017         DD2017
              B2018         F2018         J2018         N2018         R2018         V2018         Z2018         DD2018
              B2019         F2019         J2019         N2019         R2019         V2019         Z2019         DD2019

              Comment


              • #8
                Originally posted by Fei Wang View Post
                My solution is similar to Nick's. Need to put all name prefix in the list of "var".

                Code:
                local var "REVENUES EBITDA"
                local i = 0
                
                foreach str in `var' {
                forvalues j = `=`i'*7+1'(1)`=(`i'+1)*7' {
                ren v`j' `str'`=mod(`=`j'-1',7)+2013'
                }
                local ++i
                }
                I thank you!
                In your case, if I wanted to start renaming the variables from v12 (always with blocks of 7 and leaving the name of the first 11 variables unchanged), how should I proceed?
                Thanks again

                Comment


                • #9
                  Originally posted by Riccardo Busin View Post

                  I thank you!
                  In your case, if I wanted to start renaming the variables from v12 (always with blocks of 7 and leaving the name of the first 11 variables unchanged), how should I proceed?
                  Thanks again
                  My case would be

                  Code:
                  local var "REVENUES EBITDA"
                  local i = 1
                  
                  foreach str in `var' {
                      forvalues j = `=`i'*7+5'(1)`=(`i'+1)*7+4' {
                          ren v`j' `str'`=mod(`=`j'-5',7)+2013'
                      }
                      local ++i
                  }


                  William's solution in #4 becomes

                  Code:
                  local var "REVENUES EBITDA"
                  local i = 12
                  
                  foreach stub in `var' {
                         local j = `i'+6
                         rename (v`i'-v`j') (`stub'#), addnumber(2013)
                         local i = `i'+7
                     }

                  Comment


                  • #10
                    Originally posted by Nick Cox View Post
                    So, you really have 224 variables to rename.

                    Code:
                    clear
                    
                    set obs 1
                    
                    forval j = 12/235 {
                    gen var`j' = 42
                    }
                    
                    ds
                    
                    local stubs REVENUES EBITDA `c(ALPHA)' AA BB CC DD
                    
                    forval j = 12/235 {
                    if mod(`j', 7) == 5 {
                    gettoken stub stubs : stubs
                    local year = 2013
                    }
                    else local year = `year' + 1
                    
                    rename var`j' `stub'`year'
                    }
                    
                    ds
                    
                    REVENUES2013 C2013 G2013 K2013 O2013 S2013 W2013 AA2013
                    REVENUES2014 C2014 G2014 K2014 O2014 S2014 W2014 AA2014
                    REVENUES2015 C2015 G2015 K2015 O2015 S2015 W2015 AA2015
                    REVENUES2016 C2016 G2016 K2016 O2016 S2016 W2016 AA2016
                    REVENUES2017 C2017 G2017 K2017 O2017 S2017 W2017 AA2017
                    REVENUES2018 C2018 G2018 K2018 O2018 S2018 W2018 AA2018
                    REVENUES2019 C2019 G2019 K2019 O2019 S2019 W2019 AA2019
                    EBITDA2013 D2013 H2013 L2013 P2013 T2013 X2013 BB2013
                    EBITDA2014 D2014 H2014 L2014 P2014 T2014 X2014 BB2014
                    EBITDA2015 D2015 H2015 L2015 P2015 T2015 X2015 BB2015
                    EBITDA2016 D2016 H2016 L2016 P2016 T2016 X2016 BB2016
                    EBITDA2017 D2017 H2017 L2017 P2017 T2017 X2017 BB2017
                    EBITDA2018 D2018 H2018 L2018 P2018 T2018 X2018 BB2018
                    EBITDA2019 D2019 H2019 L2019 P2019 T2019 X2019 BB2019
                    A2013 E2013 I2013 M2013 Q2013 U2013 Y2013 CC2013
                    A2014 E2014 I2014 M2014 Q2014 U2014 Y2014 CC2014
                    A2015 E2015 I2015 M2015 Q2015 U2015 Y2015 CC2015
                    A2016 E2016 I2016 M2016 Q2016 U2016 Y2016 CC2016
                    A2017 E2017 I2017 M2017 Q2017 U2017 Y2017 CC2017
                    A2018 E2018 I2018 M2018 Q2018 U2018 Y2018 CC2018
                    A2019 E2019 I2019 M2019 Q2019 U2019 Y2019 CC2019
                    B2013 F2013 J2013 N2013 R2013 V2013 Z2013 DD2013
                    B2014 F2014 J2014 N2014 R2014 V2014 Z2014 DD2014
                    B2015 F2015 J2015 N2015 R2015 V2015 Z2015 DD2015
                    B2016 F2016 J2016 N2016 R2016 V2016 Z2016 DD2016
                    B2017 F2017 J2017 N2017 R2017 V2017 Z2017 DD2017
                    B2018 F2018 J2018 N2018 R2018 V2018 Z2018 DD2018
                    B2019 F2019 J2019 N2019 R2019 V2019 Z2019 DD2019
                    Thanks a lot for your help!

                    Comment


                    • #11
                      Originally posted by Fei Wang View Post

                      My case would be

                      Code:
                      local var "REVENUES EBITDA"
                      local i = 1
                      
                      foreach str in `var' {
                      forvalues j = `=`i'*7+5'(1)`=(`i'+1)*7+4' {
                      ren v`j' `str'`=mod(`=`j'-5',7)+2013'
                      }
                      local ++i
                      }


                      William's solution in #4 becomes

                      Code:
                      local var "REVENUES EBITDA"
                      local i = 12
                      
                      foreach stub in `var' {
                      local j = `i'+6
                      rename (v`i'-v`j') (`stub'#), addnumber(2013)
                      local i = `i'+7
                      }
                      Thanks a lot for your help!

                      Comment

                      Working...
                      X