Announcement

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

  • From string date to numeric date

    Hi everyone,

    I am trying to convert string dates to numeric dates. The issue is that the dates are monthly (e.g. Jan-00, Feb-00, etc.) and I entered the generate command specifying "MY" but it doesn't create any result. Thank you in advance.

  • #2
    Please read the FAQ, which says to provide the exact input commands and output from Stata.

    Did you use the monthly() function?
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

    Comment


    • #3
      The key is to specify a third argument to monthly()

      Adapt your unstated command to learn from these examples:


      Code:
      . di monthly("Jan-00", "MY")
      .
      
      . di monthly("Jan-00", "MY", 2050)
      480
      
      . di %tm monthly("Jan-00", "MY", 2050)
       2000m1

      Comment


      • #4
        The third option for monthly()s not described in help datetimes,-- it is described in help monthly--but the following is:
        Code:
        . di %tm monthly("Jan-00", "M20Y")
        2000m1
        Stata/MP 14.1 (64-bit x86-64)
        Revision 19 May 2016
        Win 8.1

        Comment


        • #5
          I introduced the commands given in answers #3 and #4. But the following happens:

          generate date2 = date(date, "MY")
          (168 missing values generated)


          Comment


          • #6
            Your command does not use the advice in #3 & #4!

            Code:
            generate date2 = monthly(date, "M20Y")
            Stata/MP 14.1 (64-bit x86-64)
            Revision 19 May 2016
            Win 8.1

            Comment


            • #7
              Repeating and expanding on Carole's advice from post #2 above, we can better help you if we know what commands you have tried and what Stata told you to indicate that there was a problem.

              Please review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. See especially sections 9-12 on how to best pose your question. It's particularly helpful to copy commands and output from your Stata Results window and paste them into your Statalist post using CODE delimiters, as described in section 12 of the FAQ.

              Comment


              • #8
                That worked. Thank you very much and sorry.

                Comment


                • #9
                  Not only does the syntax in #5 ignore the suggestions made, it is also quite incorrect in using the date() function at all. date() is for creating daily dates and cannot possibly create monthly dates directly. This is documented in the help.

                  Admittedly the name is not well chosen. It was originally created when daily dates were the only kind of date given special treatment in Stata. The synonym daily() is available and I try to emphasise its use in postings.

                  Comment

                  Working...
                  X