Announcement

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

  • Export delimited changing variable names

    export delimited has an interesting process when variables have the same name, but different cases. The data, for example, has variables named both Age and age, but also other variables that are all lower case (no variables with the exact same characters, but different case), and others that have some upper case (no variables with the exact same characters, but different case). When I export this data to .csv all variable names are converted to lower case - there is no preserve(case) option in export delimited like there is with import delimited. Then, it appears that when Stata runs into two variables that are now age and age it converts one to a generic variable name, such as v40, before saving to the csv. For various reasons, I'd much prefer to keep age and Age named as is rather than renaming all the variables that run into this issue. Has anybody else run into this and figured out a workaround?

  • #2
    You have it wrong. -export delimited- does not change the variable names here. The .csv file contains both age and Age as variable names. It is when you re-import the data into Stata using -import delimited- that the second of these variables gets renamed. But the help file for -import delimited- makes it very clear that the default with -import delimited- is to convert all variable names to lower case. This leads to a clash of two variables with the same name, which Stata resolves by changing one of the variables' names. The help also clearly shows that there is a -case()- option in -import delimited- that lets you override that default, and one of the possibilities for that is -preserve-.

    Code:
    . clear
    
    . set obs 1
    Number of observations (_N) was 0, now 1.
    
    . gen age = 10
    
    . gen Age = 20
    
    . export delimited age_case_demo, replace
    file age_case_demo.csv saved
    
    .
    . type age_case_demo.csv
    age,Age
    10,20
    
    .
    . import delimited age_case_demo, clear
    (encoding automatically selected: ISO-8859-1)
    (2 vars, 1 obs)
    
    . describe
    
    Contains data
     Observations:             1                  
        Variables:             2                  
    ----------------------------------------------------------------------------------------------------------------------------------------------
    Variable      Storage   Display    Value
        name         type    format    label      Variable label
    ----------------------------------------------------------------------------------------------------------------------------------------------
    age             byte    %8.0g                 
    v2              byte    %8.0g                 Age
    ----------------------------------------------------------------------------------------------------------------------------------------------
    Sorted by:
         Note: Dataset has changed since last saved.
    
    .
    . clear
    
    . import delimited age_case_demo, case(preserve)
    (encoding automatically selected: ISO-8859-1)
    (2 vars, 1 obs)
    
    . describe
    
    Contains data
     Observations:             1                  
        Variables:             2                  
    ----------------------------------------------------------------------------------------------------------------------------------------------
    Variable      Storage   Display    Value
        name         type    format    label      Variable label
    ----------------------------------------------------------------------------------------------------------------------------------------------
    age             byte    %8.0g                 
    Age             byte    %8.0g                 
    ----------------------------------------------------------------------------------------------------------------------------------------------
    Sorted by:
         Note: Dataset has changed since last saved.
    
    .
    Last edited by Clyde Schechter; 14 Apr 2023, 14:08.

    Comment


    • #3
      Thank you for the clarification, Clyde!

      Comment

      Working...
      X