Announcement

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

  • I'm converting a date but the new date variable is all missing values

    I'm using Stata 14.2. i have a dataset with dates in a string variable e.g. 01-Apr-2018. I have used the below code to convert them to format them as date variables, however the new date variable that I create is all missing values

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str9 dateperformed
    "2-Apr-20" 
    "2-Aug-18" 
    "16-Oct-18"
    "8-Jan-19" 
    "19-Sep-18"
    "11-Oct-18"
    "2-Jul-20" 
    "29-Oct-18"
    "24-Apr-19"
    "25-Nov-19"
    "10-Sep-19"
    "31-Jul-18"
    "29-May-18"
    "3-Jun-19" 
    "18-Dec-18"
    "18-Dec-18"
    "18-Mar-20"
    "21-May-18"
    "26-Jun-20"
    "9-May-18" 
    "21-Aug-19"
    "29-Aug-18"
    "2-Nov-18" 
    "16-Jul-20"
    "6-Apr-20" 
    "6-Apr-20" 
    "23-Jul-19"
    "18-Oct-18"
    "10-Jul-20"
    "18-Sep-19"
    "21-Mar-19"
    "16-Mar-20"
    "21-Jan-20"
    "10-Mar-20"
    "30-Jul-18"
    "29-Aug-18"
    end

    Code:
    . gen date_performed = date(dateperformed, "DMY")
    (1,483 missing values generated)
    
    . format %tdDD/NN/CCYY date_performed


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float date_performed
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    end
    format %tdDD/NN/CCYY date_performed
    I have seen similar threads and tried all the fixes I've found but nothing seems to work for my dataset. Can someone advise how to fix the formatting of this date variable?

    Thanks

  • #2
    with no century shown in your data you need to tell Stata what you want; my guess is these are all this century so try this:
    Code:
     
     gen date_performed = date(dateperformed, "DM20Y")

    Comment


    • #3
      that worked - thank you!

      Comment


      • #4
        Rich is correct and this is all documented in many threads (although there is no way that it is practical, or even an ideal, to have read them all). More crucially, see


        Code:
        help datetime
        It might seem obvious, or at least convenient, that the century would default to 20?? but Stata started in 1985 and then and for some years after it would equally have seemed obvious, or convenient, that the century should default to 19??. On the whole, it is better for users if by returning missing values Stata flags that the instruction is unclear.

        Note another syntax which is often helpful:
        Code:
         di daily("21-Jan-52", "DMY", 2050)
        -2902  
        
        . di %td daily("21-Jan-52", "DMY", 2020)
        21jan1952

        As in #1 Rich uses date() which is the historic function name dating [indeed] from a phase when daily dates were the only kind of date specially supported.

        I recommend using
        daily() which is really the same code, as a little more transparent, and to protect yourself and your readers from ,a guess that date() is a generic date function for any kind of date;
        Last edited by Nick Cox; 31 Aug 2020, 06:04.

        Comment

        Working...
        X