Announcement

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

  • Rename the list of variables with the name of each sheet*

    Hi, I am importing 3 sheets from excel and I want to rename the list of variables with the name of each sheet

    In my do I have:
    cd "D:\Do\Mat"
    local hojas "RB RM RA"
    foreach i of local hojas {
    import excel "\BDMattt2019.xlsx", sheet("`i'") firstrow clear
    ...
    ....
    ....
    save "P_`i'.dta",replace


    My vars are 20 : from INI_CDT_INT_EIB_0200_F_4 to INI_CDT_INT_EIB_4700_F_5

    Using one var it would be :

    rename INI_CDT_INT_EIB_0200_F_4 INI_CDT_INT_EIB_0200_F_4_R_`i',

    but I want to reply this in all vars(20) for each sheet.

    I want this results in my first database ( P_RB):

    Id INI_CDT_INT_EIB_0200_F_4_R_RB INI_CDT_INT_EIB_4700_F_5_R_RB
    1
    2
    3
    4
    .
    .
    .
    In my second database ( P_RM):
    Id INI_CDT_INT_EIB_0200_F_4_R_RM INI_CDT_INT_EIB_4700_F_5_R_RM
    1
    2
    3
    4
    .
    .
    .

    In my second database ( P_RA):
    Id INI_CDT_INT_EIB_0200_F_4_R_RA INI_CDT_INT_EIB_4700_F_5_R_RA
    1
    2
    3
    4
    .
    .
    .


    Kind Regards,

    S.
    Last edited by sladmin; 14 Mar 2019, 10:58. Reason: anonymize original poster

  • #2
    Inside your loop, type

    Code:
    rename (*) (*_R_`i')
    For related problems, see xls2dta (SSC), which implements the loop for you.

    Best
    Daniel

    Comment


    • #3
      Put
      Code:
      rename (INI_CDT_INT_EIB*) =_R_`i'
      inside your loop. See -help rename group- for a detailed explanation.

      Added: Crossed with #2. The solution in #2 will add the R_`i' suffix to every variable. The solution given here will rename only the INI_CDT_INT_EIB* variables.

      Comment


      • #4
        Originally posted by Clyde Schechter View Post
        Put
        Code:
        rename (INI_CDT_INT_EIB*) =_R_`i'
        inside your loop. See -help rename group- for a detailed explanation.

        Added: Crossed with #2. The solution in #2 will add the R_`i' suffix to every variable. The solution given here will rename only the INI_CDT_INT_EIB* variables.
        Thank you!

        Comment

        Working...
        X