Announcement

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

  • How to convert stringdate to date?

    Dear experts,

    I am trying to convert string-type datatype to date type
    First I tried to force the date column of csv file to numeric but it turns out that all cells are missing,

    Therefore I input the data to the Stata dta and have a sample as below

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str18 country str10 date str20 id
    "Argentina" "2021-08-31" "Argentina_1"
    "Argentina" "2021-09-01" "Argentina_1"
    "Argentina" "2021-09-02" "Argentina_1"
    "Argentina" "2021-09-03" "Argentina_1"
    "Argentina" "2021-09-06" "Argentina_1"
    "Argentina" "2021-09-07" "Argentina_1"
    "Argentina" "2021-09-08" "Argentina_1"
    "Argentina" "2021-09-09" "Argentina_1"
    "Argentina" "2021-09-10" "Argentina_1"
    end
    I have tried a couple of approaches around following some topics here but there is nothing works so far.

    Code:
    generate numdate = date(date, "MDY")
    format numdate %td
    Click image for larger version

Name:	1.PNG
Views:	1
Size:	8.2 KB
ID:	1692438



    Code:
    generate numdate1 = date(date, "MDY")
    Click image for larger version

Name:	2.PNG
Views:	1
Size:	8.9 KB
ID:	1692439


    Many thanks and best regards,


  • #2
    Your dates are YMD not MDY.

    Comment


    • #3
      the date format is year-month-day,
      Code:
      gen numdate = date(date, "YMD")

      Comment


      • #4
        Dear Nick Cox and @Øyvind Snilsberg, I got it now, problem solved

        Thank you

        Comment

        Working...
        X