Announcement

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

  • From string date "DD/MM/YYYY" to month and year variables

    Hi,

    How can I convert strings like "01/03/1997", e.i. of type "DD/MM/YYYY", to a new numeric variable for the month and one for the year?

    I would like to have a variable with the value 3 (month) and one with the value 1997 (year).

    Many thanks.

  • #2
    Code:
    gen year = yofd(date(datevar,"DMY"))
    gen month = mofd(date(datevar,"DMY"))
    Code:
    gen year = real(substr(datevar,7,4))
    gen month = real(substr(datevar,4,2))
    Last edited by Øyvind Snilsberg; 05 Nov 2021, 03:58.

    Comment


    • #3
      Thanks a lot! :-)

      Comment


      • #4
        #2 is very helpful but to extract month of year rather than monthly date from a daily date you need month() not mofd().

        Comment

        Working...
        X