Announcement

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

  • Extracting subset date from a timestamp date variable

    Dear Colleagues

    Please help: I am trying to extract the first 8 characters (NEWdate) from a date format (OLDdate) which has timestamp in it.
    OLDdate's format is %tc

    OLDdate
    14jan2011 00:00:00


    NEWdate
    14jan2011

    I have tried the two commands below and they dont seem to work:

    gen NEWdate = substr(OLDdate, 1, 9))
    format NEWdate %td


    gen NEWdate = dt(OLDdate_Date, 1, 9)
    format NEWdate %td


    Thanks for your help.

    Regards

    Fadzai


  • #2
    Stata's "date and time" variables are complicated and there is a lot to learn. If you have not already read the very detailed Chapter 24 (Working with dates and times) of the Stata User's Guide PDF, do so now. If you have, it's time for a refresher. After that, the help datetime documentation will usually be enough to point the way. You can't remember everything; even the most experienced users end up referring to the help datetime documentation or back to the manual for details. But at least you will get a good understanding of the basics and the underlying principles. An investment of time that will be amply repaid.

    All Stata manuals are included as PDFs in the Stata installation (since version 11) and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.

    Your problem, which the reading will help you understand, is that you have mistaken the presentation of your OLDdate for its value. OLDdate is a Stata Internal Format datetime numeric - not string - variable. The format %tc causes the number to be represented in what is called Human Readable Form as you see it. Here is an example of the solution to your problem, but do not fail to do the reading.
    Code:
    . generate NEWdate = dofc(OLDdate)
    
    . format NEWdate %td
    
    . list
    
         +--------------------------------+
         |            OLDdate     NEWdate |
         |--------------------------------|
      1. | 14jan2011 00:00:00   14jan2011 |
         +--------------------------------+
    
    . describe OLDdate NEWdate
    
                  storage   display    value
    variable name   type    format     label      variable label
    --------------------------------------------------------------------------------
    OLDdate         double  %tc                  
    NEWdate         float   %td                  
    
    . // what do the dates look like as numbers?
    . format OLDdate NEWdate %20.0f
    
    . list
    
         +-------------------------+
         |       OLDdate   NEWdate |
         |-------------------------|
      1. | 1610582400000     18641 |
         +-------------------------+
    
    .
    Last edited by William Lisowski; 07 Jun 2020, 20:29.

    Comment


    • #3
      Thanks for the quick response, that's great. I will invest some time in reading "
      Working with dates and times" in STATA

      Comment

      Working...
      X