Search code examples
datestata

Date format in Stata mismatch error


Here is the format of the original date : 01/31/2016 (MDY)

I want to change the format of this date

gen date = date(Date, "MDY")  //Date is the varname of my original date

format date %td

Error : type mismatch r(109)


Solution

  • This works for me

    clear
    set obs 1 
    gen Date = "01/31/2016"
    gen date = date(Date, "MDY")  //Date is the varname of my original date
    format date %td
    list 
    
         +------------------------+
         |       Date        date |
         |------------------------|
      1. | 01/31/2016   31jan2016 |
         +------------------------+
    

    so I guess you're doing something different and not telling us about it.

    That is,

    1. The result of date() if it executes without error can only be a numeric variable containing integers.

    2. So it will take a daily date format without complaint.

    Your error message implies that date is string.

    If you really tried to format Date %td, then that could be the error.

    Alternatively, if Date is a numeric variable with display format %tdN/D/Y so that a numeric date meaning 31 January 2016 would display as 01/31/16 then to change its format you need not and cannot it push it through date(): you just change the format with format Date %td.

    You seem close to the fallacy that display format means storage type. See this paper for a refutation of that idea.