Announcement

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

  • How to extract month from an iso date?

    I have a date in the following iso format:

    ISODATE
    2017-09-01 10:43:04.000

    The format is Stata is: %tcCCYY-NN-DD_HH:MM:SS.sss

    I'm trying to extract out the month but haven't been able to make it work.

    I tried various forms such as:
    gen month=month(ISODATE)

    Appreciate some help here. thanks.

  • #2
    When you say you want to extract the month, do you want "September 2017" or just "September?"

    The first is:

    Code:
    gen month_year = mofd(dofc(ISODATE))
    format month_year %tm
    The second is:

    Code:
    gen month = month(dofc(ISODATE))
    The key, in either case, is that both the -mofd()- and -month()- functions take a Stata date variable as an argument. But your ISODATE, despite its name, is not a date variable: it's a clock variable. So those functions give you nonsensical results. The trick is to first extract the date from the clock variable, using the -dofc()- function.

    Comment


    • #3
      When Clyde Schechter says date variable he naturally means daily date variables. Stata does support weekly, monthly, quarterly, half-yearly and yearly date variables as well as daily date variables -- and date-time variables.

      In this ambiguity Stata too sometimes follows ordinary use. For example, if you wanted to know the date of Clyde's birthday you would think odd if he replied (say) quarter 2 or April. You'd expect a (daily) date.

      The reason Stata uses date() as the name of the function to extract daily dates from strings is more easily explained by history. Daily dates were for some releases the only kind of date given special support by Stata (noting that yearly dates such as 1999 or 2003 hardly need much special support). The other kinds of dates followed later.

      Back to the main point: mofd() is to be parsed as yielding the monthly date reduction of a daily date and in turn

      Code:
      help month()
      explains that it maps daily dates to month of year.

      Comment

      Working...
      X