Search code examples
excelstatatype-inferencedate-conversion

Stata to Excel: Keeping the time variable correct


I am trying to export a .dta file of mine from Stata to Excel. The file consists an important time variable. Though it shows fine in Stata, in the format of %tm (2010m12 for example), when I export it to Excel it shows numbers. Tried to export it as csv, xls, xlsx, but doesn't seem to help. Example:

On Stata:

On Excel after exporting:

I also tried to play with the formats and types (it's float originally) but couldn't fix it. Didn't find any solution for this.


Solution

  • It is not clear to me why Stata exports %tm-formatted variables in this way. For example, 2010m2 is converted as 21916.000006956 when labeled, and as 601 when not.

    However, this will export the %tm date as a string:

    clear
    set obs 10
    gen time = _n+600
    format time %tm
    
    export excel using "time.xls", replace datestring("%tm")