Announcement

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

  • Destring dates

    Dear all,
    It might be a very easy question but I am struggling.

    I have a variable date of birth in the format dd.mm.yyyy (for example 01.01.2023), that stata sees as string.

    I tried to destring it but I get that:

    destring dob_baby, replace
    dob_baby: contains nonnumeric characters; no replace

    I checked, entries have the same format.

    How can I obtain a stata date format?

    Many thanks!

    Manon

  • #2
    Use the daily() or date() function here. At best if you ignore the dots or periods or stops you will get integers like 1012023 which are not even in the right order.

    Code:
    help datetime
    is essential reading. Skip and skim
    what is not directly relevant right now.

    Comment


    • #3
      Try the -date()- function.

      Something like

      Code:
      gen dob_baby_stata = date(dob_baby, "DMY")

      Comment


      • #4
        That is a big topic, mainly because dates are written differently across countries and within countries (e.g. what comes first the day or the month?). So Stata would need to accommodate all of that, and that gets complicated. The helpfile to look for is help datetime .

        Lets start an example, and input some dates as a string.

        Code:
        clear
        input str10 dob_baby
        "01.01.2023"
        "13.01.2023"
        end
        I want to create a new variable (dob) that takes those strings and transforms that into a Stata date

        Code:
        gen dob = date(dob_baby, "DMY")
        That is what the date() function is for. The first argument is the string, the second argument the "mask", i.e. in what order are the parts of the date stored. "DMY" means Day Month Year.

        We can now admire the result:

        Code:
        . list
        
             +--------------------+
             |   dob_baby     dob |
             |--------------------|
          1. | 01.01.2023   23011 |
          2. | 13.01.2023   23023 |
             +--------------------+
        OK, so that looks weird. But it makes sense: Stata dates are the number of days since 1.1.1960, so 1.1.2023 is 23,011 days since 1.1.1960. Stata can work with that, but I can't. Fortunately, we can let Stata display the Stata date as a date we humans can understand while its actual value is one Stata can work with.

        Code:
        . format dob %td
        
        . list
        
             +------------------------+
             |   dob_baby         dob |
             |------------------------|
          1. | 01.01.2023   01jan2023 |
          2. | 13.01.2023   13jan2023 |
             +------------------------+
        There are a lot more things you can do with dates (and time), which are in the help file I mentioned, but this example should get you started.
        ---------------------------------
        Maarten L. Buis
        University of Konstanz
        Department of history and sociology
        box 40
        78457 Konstanz
        Germany
        http://www.maartenbuis.nl
        ---------------------------------

        Comment


        • #5
          Some excellent development of the point in #2 about using date() or daily() as functions designed for the purpose. It's the same code.

          Let me expand on the point that destring is quite wrong here. (I have no bias against destring when used as intended.)

          destring would do something here if and only if you get it to ignore the stops (dots, periods, whatever you want to call them). But the result is useless, and as said not even in the right order.

          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input str10 wehave
          "01.01.2023"
          "02.01.2023"
          "13.01.2023"
          "01.03.2022"
          end
          
          . destring wehave, gen(badidea) ignore(".")
          wehave: character . removed; badidea generated as long
          
          . l
          
               +-----------------------+
               |     wehave    badidea |
               |-----------------------|
            1. | 01.01.2023    1012023 |
            2. | 02.01.2023    2012023 |
            3. | 13.01.2023   13012023 |
            4. | 01.03.2022    1032022 |
               +-----------------------+

          Comment

          Working...
          X