Announcement

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

  • Renaming parts of the variable name

    Hi,
    I have two data sets, that I want to append. They contain the same variables and information. However, the variables are named differently in both data sets. In data set A variables are named: v31002, v31003, v31004..... In data set B variables are named: v31202, v31203, v31204....
    I now want to rename the variables from data set B, so that they are the same as in data set A. Hence, I want to replace the 2 (or: the 4th digit of each variable name) with a 0. So my question only refers to the name of the variable, not the content.
    Is there a possiblity to do this with a loop? So that I don“t have to rename every variable individually?

    Many thanks and best,
    Wiebke

  • #2
    1. Look at the help.

    Code:
    help rename groups
    2. Experiment.

    Code:
    clear 
    set obs 1
    foreach v in v31002 v31003 v31004 { 
        gen `v' = 42
    }
    
    ds 
    
    rename (v310*) (v312*) 
    
    ds
    
    v31202  v31203  v31204

    Comment


    • #3
      Something like

      Code:
      rename ( v31(#)(##) )( v310.(##) )
      should work.

      Nick's code in #2 is simpler; might be less safe if there are variables matching v31* that you do not want to rename.

      Best
      Daniel
      Last edited by daniel klein; 29 May 2018, 06:27.

      Comment


      • #4
        Thanks a lot!! It worked

        Comment

        Working...
        X