Announcement

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

  • Proper import column names from excel table

    Dear all,

    I try to import the table from Excel using the code below:
    Code:
    set maxvar 5700, permanently
    import excel using excess.xlsx , firstrow
    The first row in Excel contains column names consisting of the numbers like 100003 and so on.
    However, Stata imports the table, taking these numbers labels and gives variable names like "A", "B", and so on.

    When I try to rename them so that labels become names of variables, Stata cannot do that:

    Code:
    foreach v of varlist A B C {
    local x : variable label `v'
    *local q`v'  =strtoname("`x'",0) - 1st attempt
    local q`v'  =int(`x') - 2nd attempt
    ren `v' `q`v''
    label variable `q`v'' "`v'"
    }
    Could you please help me to either import the table so that integer of the first row were imported as variable names or to change labels to names.
    Thanks a lot.

  • #2
    You will need some proper string value for the name, for example by adding 'v' to each number before using it as the name

    Code:
    foreach v of varlist A-C { 
    local x : variable label `v' 
    ren `v' v`x'
    }
    Just doign this will preserve original number format from excel as the variable label

    Comment

    Working...
    X