Announcement

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

  • YYYYMMDD date variable

    Hello
    I have a date variable in% tc format. How can I create a new numeric variable of type (not format) YYYYMMDD? (for example: 20200822). Thank you

  • #2
    %tc is a datetime format so you have Stata datetime variable. What you seem to want is a Stata daily variable as this produces.
    Code:
    generate wanted = dofc(have)
    format wanted %tcCCYYNNDD
    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.

    Comment


    • #3
      If you really want an 8 digit integer based on a daily date you can get it:

      Code:
      . di %8.0f  real(strofreal(mdy(8, 23, 2020), "%tdCYND"))
      20200823
      but the only reason I can think of for that would be that it is required or convenient for something outside Stata. In generating such a variable make sure to use an appropriate storage type and display format.

      Comment

      Working...
      X