Announcement

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

  • converting string date in MM/DD/YYYY format to Stata date

    Hello,
    I wish to convert these string dates, in which the MM and DD may be either 1 or two digits. The following two commands have been unsuccessful:

    todate mydate, gen(statadate) p(ddmmyyyy)
    returns error "length does not match pattern"

    gen statadate = date(mydate, "DDMMYYYY")
    generates missing values

    Any advice/corrections would be most appreciated.






  • #2
    You have answered your own question about todate (SSC) as you know that the pattern you have specified does not match all the data, which is also conveyed by the error message.

    I think you may have to show us examples but otherwise if your dates are MDY format, as the title implies, then it would be quite wrong to specify even DMY as an argument to date().

    Comment


    • #3
      Hello
      I wish to covert my date format from dd/mm/yyyy to "yyyyQq". for example "25/12/1999" to "1999Q4" or "99Q4". When i tried to change it, it just converted to a number with q4. Any advice please!!! Thanks

      Comment


      • #4
        The advice is always to show your data, not talk about it, and to show the exact syntax you used.

        Please read and act on https://www.statalist.org/forums/help#stata

        #3 could mean different things. It's your job to tell us which.

        People often focus on changing a date format when the real issue is changing the date type. See e.g. https://www.stata-journal.com/sjpdf....iclenum=dm0067

        Comment


        • #5
          Hello,

          I am trying to use the syntax (is that the right word to use?) todate SSC, and I have a string which is in the format: YYYYMMDDHHMMSS.

          I was thinking perhaps I could use todate to convert it to extract only the year out of it, however, I am getting the pattern mismatch code as I believe that only YYYYMMDD is accepted. Am I correct in my belief that todate cannot handle such a long clock format.

          Do you have any suggestions for what other Stata codes/syntax I could use to help with this?

          thanks

          Comment


          • #6
            todate was written for Stata 8 in 2005 and does not support date-time variable creation. The help file doesn't say so but when the help file was written such variables did not exist.

            You should show us sample strings as there is as yet no evidence that clock() will not work for you in reading in date-times after which year extraction will be easy. Contrariwise if year really does define the first 4 characters then

            Code:
            gen year = real(substr(whatever, 1, 4))
            should get you there directly.
            Last edited by Nick Cox; 25 Nov 2019, 07:08.

            Comment


            • #7
              Dear Nick,

              Thank you so much.

              Good to know about the history of todate. To get the year out of the original variable, svytimestamp2, I tried this:

              Code:
              gen svytimestamp2=substr(svy_timestamp,1,4)
              destring svytimestamp2, replace
              and it worked, moving forward, I will use your code. I am assuming the real before the substr takes care of the destring, is this correct?

              Comment


              • #8
                Correct.

                Comment


                • #9
                  Great! Thank you

                  Comment

                  Working...
                  X