Announcement

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

  • Separating dates

    I am working on a dataset which has date in the following format, I want separate columns for month, date and year can anyone help me with the relevant command please? Thank you in advance.

    dataex interviewdate

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long interviewdate
     .
    29
    29
     .
     .
     .
     .
    24
     .
     .
    27
     .
     .
     .
     .
     .
    47
    47
     .
    37
    14
    37
     .
     .
    29
    29
     .
     .
     .
    15
     .
    32
     .
     .
     .
     .
    15
     .
    15
     .
     .
    15
     .
     .
    42
    42
    42
     .
     .
     .
     .
    42
    42
     .
     .
    32
     7
     .
     7
     .
     .
    20
    20
     .
    20
    20
    20
    15
     .
     .
     .
     .
     .
     .
    13
    13
     .
    13
    13
     .
    13
     .
     .
     .
     7
     .
     .
     7
     7
     .
     7
     .
    19
    19
    19
    19
    19
    19
    19
    19
    end
    label values interviewdate interviewdate
    label def interviewdate 7 "11/3/2004", modify
    label def interviewdate 13 "16/02/04", modify
    label def interviewdate 14 "16/03/04", modify
    label def interviewdate 15 "17/02/04", modify
    label def interviewdate 19 "18/03/04", modify
    label def interviewdate 20 "19/02/04", modify
    label def interviewdate 24 "21/02/04", modify
    label def interviewdate 27 "23/02/04", modify
    label def interviewdate 29 "24/02/04", modify
    label def interviewdate 32 "25/02/04", modify
    label def interviewdate 37 "28/02/04", modify
    label def interviewdate 42 "4/3/2004", modify
    label def interviewdate 47 "6/3/2004", modify
    ------------------ copy up to and including the previous line ------------------


  • #2
    The only thing that is a little tricky is that you have two digit and four digit years. I am assuming that any two digit years (like "04") should be in this century ("2004").

    Code:
    *create a new var date that is the label of interview date
    decode interviewdate, gen(date)
    
    *split date into parts
    split date, parse("/") gen(d_) destring
    
    *fix the 2 digit years; change to 4 digit years
    replace d_3=real(string(2)+string(d_3, "%03.0f")) if d_3<1000
    
    *rename the variables
    rename d_1 day
    rename d_2 month
    rename d_3 year
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

    Comment


    • #3
      The command worked perfectly, thank you so much.

      Comment


      • #4
        Carole gives excellent advice as you asked. For most Stata purposes, however, you'd be better off with a Stata daily date.

        Code:
        gen ddate = mdy(month, day, year) 
        format ddate %td

        Comment


        • #5
          The command Carole gave worked with the first dataset but in the second data set it isn't working I am sharing the interview date example below
          dataex interview date

          ----------------------- copy starting from the next line -----------------------
          [CODE]
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input str9 interviewdate
          "15-Jan-05"
          ""
          "15-Jan-05"
          ""
          "15-Jan-05"
          "15-Jan-05"
          ""
          "15-Jan-05"
          "15-Jan-05"
          ""
          "15-Jan-05"
          "15-Jan-05"
          "15-Jan-05"
          "15-Jan-05"
          ""
          "15-Jan-05"
          "15-Jan-05"
          ""
          "15-Jan-05"
          ""
          "15-Jan-05"
          ""
          "15-Jan-05"
          "15-Jan-05"
          "15-Jan-05"
          "12-Jan-05"
          ""
          "12-Jan-05"
          ""
          "12-Jan-05"
          "12-Jan-05"
          ""
          "12-Jan-05"
          ""
          ""
          "12-Jan-05"
          ""
          ""
          "12-Jan-05"
          "12-Jan-05"
          ""
          "12-Jan-05"
          ""
          "12-Jan-05"
          "12-Jan-05"
          ""
          ""
          ""
          "12-Jan-05"
          "12-Jan-05"
          ""
          ""
          ""
          "12-Jan-05"
          "12-Jan-05"
          ""
          ""
          ""
          "12-Jan-05"
          ""
          "12-Jan-05"
          ""
          ""
          "12-Jan-05"
          "12-Jan-05"
          ""
          ""
          "10-Jan-05"
          ""
          ""
          "10-Jan-05"
          ""
          ""
          "10-Jan-05"
          "10-Jan-05"
          ""
          ""
          --more--

          Comment


          • #6
            It really isn't fair comment that a solution designed for a quite different problem doesn't work with the data shown in #5.

            Just read please

            Code:
            help datetime
            to learn how to convert string dates into Stata numeric daily dates. You can't hope to deal with dates successfully in Stata without a minimal understanding of those principles.

            Comment


            • #7
              I agree with Nick. If you are going to be working with dates, it is essential to understand the way that Stata represents dates. Once you have accomplished the task of turning your string that is in HRF to a SIF (the datetime help file will explain these terms) then you can then use the functions:

              Code:
              gen day=day(new_date_var)
              gen month=month(new_date_var)
              gen year=year(new_date_var)
              Stata/MP 14.1 (64-bit x86-64)
              Revision 19 May 2016
              Win 8.1

              Comment


              • #8
                Okay, thanks I will read the suggested material and run the commands.

                Comment

                Working...
                X