Announcement

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

  • Trouble with formatting dates

    Hello all,

    I am having trouble remembering how I formatted dates to look like this:


    date2 under50 over100
    201306 2 2.19
    201307 2.135 2
    201308 2 2.19
    201309 1.9 2.2
    201310 1.43 2.42
    201311 1.59 2.13
    201312 1.615 2.39
    201401 2 2.54
    201402 2.3249998 2.57
    201403 2.45 2.79
    201404 1.38 2.695
    201405 2.12 2.45
    201406 2.23 2.845
    201407 2 2.45
    201408 1.785 2.81
    201409 1.69 2.59
    201410 1.9 2.53
    201411 2.395 2.97
    201412 2 2.54
    201501 2 2.65
    201502 1.81 2.96
    201503 2 2.94
    201504 2 2.675
    201505 2 2.695
    201506 2.025 2.75
    201507 2 2.98
    201508 1.75 3
    201509 1.63 2.71
    201510 2 2.71
    201511 2 2.61
    201512 1.42 2.52
    201601 1.55 2.54
    201602 1.345 2.55
    201603 1.46 2.62
    201604 1.4 2.485
    201605 1.86 2.54
    201606 1.465 2.8
    201607 2 2.5
    201608 2.19 2.5
    201609 1.81 2.49
    201610 2 2.33
    201611 1.66 2.435
    201612 1.825 2.62
    201701 2 2.2849998
    201702 1.465 2.71
    201703 1.99 2.7
    201704 2.54 2.62
    201705 2 2.81
    201706 2.37 2.71
    201707 2.42 2.705
    201708 2 2.905
    201709 2 2.71
    201710 2.12 2.77
    201711 2.145 2.83
    201712 2.52 2.98
    201801 2.66 2.785
    201802 2.81 2.76
    201803 2.31 2.88
    201804 2 3
    201805 2.21 2.82
    201806 2.565 2.81
    201807 2 2.81
    201808 2.345 2.98
    201809 2 3
    201810 2 3
    201811 1.92 2.67
    201812 2 2.92
    201901 1.69 2.98
    201902 2 2.78
    201903 2.19 2.98
    201904 1.685 2.89
    201905 2 2.79
    201906 1.7 2.79
    201907 1.76 2.79
    201908 1.89 2.69
    201909 2 2.98
    201910 1.53 2.61
    201911 1.555 2.89
    201912 2 2.775
    202001 1.535 2.98
    202002 2 2.81
    202003 1.735 2.25
    202004 1.31 2
    202005 1 2
    202006 1.0799999 2
    202007 1.51 2
    202008 1.29 2.2150002
    202009 1.19 2
    202010 1.29 2
    202011 1.16 2.3
    202012 1.33 2.28
    202101 1.43 2.675
    202102 1.29 2.71
    202103 1.19 2.54
    202104 1.57 2.975
    202105 2 2.98
    202106 2 3
    202107 2 3
    end

    twoway line median median2 date2, title("One Year Ahead Median Expected Earnings Growth", size(medium)) lcolor(blue%50 red%50) ytitle("") xtitle("") ylabel(0 .5 1 1.5 2 2.5 3 3.5 4 4.5 5,grid) xlabel(,format(%tdCY)) legend(size(small) pos(6) row(1) lab(1 "Income under 50k") lab(2 "Income over 100k")) graphregion(fcolor(white))

    this is the code but it is not working. any help would be appreciated.
    Attached Files

  • #2
    Please use .png not .pdf attachments for images. This is explained at FAQ Advice #12.

    The brute fact is that monthly dates like this are good for people, who can read them quite easily, but lousy for analysis, as each series of 12 monthly dates taken literally (which means numerically) implies 11 gaps of 1 and 1 of 89, which can only look bizarre on a graph. So integers like 201712 and 201801 just aren't going to be decoded inside Stata as you want.

    Also, the function monthly() is not smart enough to deal with them, even when you present it with a string equivalent.

    Code:
    . di monthly("202211", "YM")
    .
    Some surgery is needed as in

    Code:
    clear
    input date2 under50 over100
    201307 2.135 2
    201308 2 2.19
    201309 1.9 2.2
    201310 1.43 2.42
    201311 1.59 2.13
    201312 1.615 2.39
    201401 2 2.54
    201402 2.3249998 2.57
    201403 2.45 2.79
    201404 1.38 2.695
    201405 2.12 2.45
    201406 2.23 2.845
    201407 2 2.45
    201408 1.785 2.81
    201409 1.69 2.59
    201410 1.9 2.53
    201411 2.395 2.97
    201412 2 2.54
    end
    
    gen mdate = ym(floor(date2/100), mod(date2, 100))
    format mdate %tm
    
    
    list mdate, sep(6)
    
         +---------+
         |   mdate |
         |---------|
      1. |  2013m7 |
      2. |  2013m8 |
      3. |  2013m9 |
      4. | 2013m10 |
      5. | 2013m11 |
      6. | 2013m12 |
         |---------|
      7. |  2014m1 |
      8. |  2014m2 |
      9. |  2014m3 |
     10. |  2014m4 |
     11. |  2014m5 |
     12. |  2014m6 |
         |---------|
     13. |  2014m7 |
     14. |  2014m8 |
     15. |  2014m9 |
     16. | 2014m10 |
     17. | 2014m11 |
     18. | 2014m12 |
         +---------+
    As always the essential reading is

    Code:
    help datetime
    help datetime display formats
    But all that said, your graph shows monthly data for about 8 years and on that scale years are definitely intervals, not points. So what is the reader supposed to think about the tick for 2013? Does it represent the start, the end, or the middle of 2013? A better approach is explained at https://www.stata-journal.com/articl...article=gr0030



    Last edited by Nick Cox; 25 Nov 2022, 06:21.

    Comment

    Working...
    X