Announcement

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

  • Formatting Date

    Dear Statalist, i hope everyone is fine.

    i want to convert date from formate YYYYMMDD to DD/MM/YYYY. I am sorry but i have no idea how to deal this formate dates.

    thanks

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long date
    19851231
    19860131
    19860228
    19860331
    19860430
    19860530
    19860630
    19860731
    19860829
    19860930
    end

  • #2
    Code:
    gen double wanted = date(string(date,"%8.0f"),"YMD")
    format %td wanted

    Comment


    • #3
      Here's another way to do it. See https://www.stata-journal.com/articl...article=dm0058 for the functions every long-term user is likely to need.

      What's more important: these look like monthly dates in disguise!

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input long date
      19851231
      19860131
      19860228
      19860331
      19860430
      19860530
      19860630
      19860731
      19860829
      19860930
      end
      
      gen year = floor(date/10000)
      gen md = mod(date, 10000)
      gen month = floor(md/100)
      gen day = mod(md, 100)
      
      gen ddate = mdy(month, day, year)
      format ddate %td 
      
      gen mdate = mofd(ddate)
      format mdate %tm 
      
      list 
      
          +------------------------------------------------------------+
           |     date   year     md   month   day       ddate     mdate |
           |------------------------------------------------------------|
        1. | 19851231   1985   1231      12    31   31dec1985   1985m12 |
        2. | 19860131   1986    131       1    31   31jan1986    1986m1 |
        3. | 19860228   1986    228       2    28   28feb1986    1986m2 |
        4. | 19860331   1986    331       3    31   31mar1986    1986m3 |
        5. | 19860430   1986    430       4    30   30apr1986    1986m4 |
           |------------------------------------------------------------|
        6. | 19860530   1986    530       5    30   30may1986    1986m5 |
        7. | 19860630   1986    630       6    30   30jun1986    1986m6 |
        8. | 19860731   1986    731       7    31   31jul1986    1986m7 |
        9. | 19860829   1986    829       8    29   29aug1986    1986m8 |
       10. | 19860930   1986    930       9    30   30sep1986    1986m9 |
           +------------------------------------------------------------+
      .

      Comment


      • #4
        thanks allot, both commands works perfectly.

        Comment

        Working...
        X