Announcement

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

  • Date variable conversion from string format

    Hi everyone, I have date variables in the following format - yyyymmdd_hhmmss

    I've looked at the existing date variable conversion threads and can't find one that helps for this specific type of format. Here's examples of the strings in the variable

    TIMESTAMP
    20210219_193618
    20210219_195936
    20210220_143856
    20210220_161023
    20210221_154242
    20210221_161101
    20210221_172955
    20210221_181902
    20210221_185542
    20210221_192824

    As you can see the format has the year, month then day, followed by an underscore, then the hours, minutes and seconds.

    How can I convert this to a date format? I've tried the following code -

    Code:
    generate voucher_date=clock(TIMESTAMP, "yyyymmdd_hhmmss")
    and it produces missing values.

  • #2
    Code:
    gen double voucher_date = clock(TIMESTAMP, "YMDhms")
    format %tc voucher_date

    Comment


    • #3
      Thanks Oyvind ! I had a similar problem.

      Comment


      • #4
        Code:
        help datetime
        remains the documentation that people concerned with dates and times should look at sooner or later. The good news when you have a specific problem is that most of it can be skimmed or skipped as being about other specific problems. But nothing there or in any threads based on understanding it supports the idea that
        Code:
        yyyymmdd_hhmmss
        could be an argument to
        clock(),

        Comment


        • #5
          Hello,
          I'm having trouble converting data in date format again. The following code, proposed by Oyvind was working so far:

          gen double date1 =clock(date_s,"DMYhms")
          format %tc date1

          For all the questionnaires collected in January however, the code produces missing values.

          The variable date_s is in the following format

          29 dec. 2021 10:25:52
          6 janv. 2022 10:53:15

          Any thought?

          Thanks,

          Comment


          • #6
            Code:
            replace date_s = subinstr(date_s,"janv","jan",.)
            
            gen double date1 = clock(date_s,"DMYhms")
            format %tc date1

            Comment


            • #7
              Edits for several other months seem likely. Stata isn't up to months in French if their abbreviations differ much from

              Code:
              . di "`c(Mons)'"
              
              Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec

              Comment


              • #8
                To add information the variable date_s has been created from an original variable with accent (survey conducted in French) with the following code:

                capture drop date_s
                gen date_s = ustrlower( ustrregexra( ustrnormalize( submissiondate, "nfd" ) , "\p{Mark}", "" ) )
                recast str22 date_s

                Comment


                • #9
                  Code:
                  gen mois = word(date_s, 2 ) 
                  
                  tab mois

                  Comment


                  • #10
                    Great! It worked! Thanks a lot for your swift answers.

                    Comment

                    Working...
                    X