Announcement

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

  • Parsing Numeric Date Time Format

    Hello,

    I am new to Stata and am working with a pre-existing dataset. I am trying to figure out how a particular numeric date-time variable was converted from a string variable d_date_start_s, as I want to use the same method to convert a new set of string date-time values into numeric form. An example of the string value format is "Jan 27, 2023 7:38:03 am", which has the corresponding numeric value 23037. I tried to construct a new numeric variable from d_date_start_s to see if it would give me the same format by running

    generate double numvar = clock(d_date_start_s, "MDYhms")

    but this just generated missing values.

    As far as I can tell, the Datetime Conversion manual doesn't include this exact string date/time format so any help would be appreciated.

  • #2
    Well, 23037 is not the numeric value corresponding to the date-time Jan 27, 2023 7:38:03 am. It is the numeric value corresponding to just the date Jan 27,2023.

    I tried to construct a new numeric variable from d_date_start_s to see if it would give me the same format by running

    generate double numvar = clock(d_date_start_s, "MDYhms")

    but this just generated missing values.
    I cannot replicate this in my setup. This code, with the example value of d_date_start_s "Jan 27, 2023 7:38:03 am" gives me a non-missing numeric value which, when formatted %tc, displays correctly as Jan 27 2023 7:38:03 am:

    Code:
    .    * Example generated by -dataex-. For more info, type help    dataex
    .    clear
    
    .    input str23 str_date_time
    
        str_date_time
        1. "Jan 27, 2023 7:38:03 am"
        2. end
    
    .
    .    gen double num_date_time = clock(str_date_time, "MDYhms")
    
    .    list
    
        
        str_date_time   num_dat~e
        -
        1.  Jan 27, 2023 7:38:03 am   1.990e+12
        
    
    .    format num_date_time %tc
    
    .    list
    
        
        str_date_time        num_date_time
        -
        1.  Jan 27, 2023 7:38:03 am   27jan2023 07:38:03
        
    
    .
    To get to the desired result, 23037, one need only then extract the date:
    Code:
    . gen num_date = dofc(num_date_time)
    
    . list
    
         +---------------------------------------------------------+
         |           str_date_time        num_date_time   num_date |
         |---------------------------------------------------------|
      1. | Jan 27, 2023 7:38:03 am   27jan2023 07:38:03      23037 |
         +---------------------------------------------------------+
    
    . format num_date %td
    
    . list
    
         +----------------------------------------------------------+
         |           str_date_time        num_date_time    num_date |
         |----------------------------------------------------------|
      1. | Jan 27, 2023 7:38:03 am   27jan2023 07:38:03   27jan2023 |
         +----------------------------------------------------------+
    
    .

    Comment


    • #3
      Ah, thank you so much! I just needed to use the format command. Thanks also for showing me how to extract the date.

      Comment

      Working...
      X