Announcement

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

  • format date variable

    Hello,

    i have a date variable in my dataset which is type "long" and formated in the following way:

    date
    28jun2017
    04oct2017
    09sep2014
    11jan2016


    I would like to change the formatting to make it look like this:

    date
    28-jun-2017
    04-oct-2017
    09-sep-2014
    11-jan-2016

    I tried this before:
    tostring date, format("%09.0f") replace
    gen date1=substr(date,1,2)+"-"+substr(date,3,9)
    as a first step for the first "-"

    but the "tostring" command converts the variable in numbers only, and the date information is somehow lost.
    If i try tostring date, format("%09.0f") replace, the variable suddenly looks like this:
    date
    000020998
    000021096
    000019975
    000020464

    I hope someone can help me ! thank you !

    PS: i tried to display the data with dataex but that also just gave me really weird looking values for the date variable.



  • #2
    Display format for dates is not about converting them to string variables, which is a giant step backwards.

    The "really weird looking values" should have looked something like this


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float date
    20998
    21096
    19975
    20464
    end
    format %td date
    and they do make sense. The minimal understanding needed here is documented prominently, e.g. at

    Code:
    help datetime
    
    help datetime display formats
    1. Stata much prefers to hold daily dates in numeric variables and as integers such that 0 is 1 January 1960.

    2. To see such dates in conventional terms you need to issue a format command -- and there are many formats to choose from.

    In this case,


    Code:
    . format date %tdDD-mon-CCYY
    
    . l
    
         +-------------+
         |        date |
         |-------------|
      1. | 28-jun-2017 |
      2. | 04-oct-2017 |
      3. | 09-sep-2014 |
      4. | 11-jan-2016 |
         +-------------+
    Last edited by Nick Cox; 03 Dec 2021, 03:11.

    Comment


    • #3
      thank you so much !! very helpful and i somehow have missed this!
      help datetime display formats

      Comment


      • #4
        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 and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.

        Comment

        Working...
        X