Announcement

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

  • Year Month From Numeric Variable

    I want to reformat a time series variable.
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte cmdCode long period str235 cmdDescE double TradeValue
    1 201001 "Animals; live" 10.743831
    1 201002 "Animals; live"  5.734226
    1 201003 "Animals; live"   .033059
    1 201004 "Animals; live"      .024
    1 201005 "Animals; live"  9.001487
    1 201006 "Animals; live"   .042875
    1 201007 "Animals; live"     .0384
    1 201008 "Animals; live" 19.492008
    1 201009 "Animals; live"     .3085
    1 201010 "Animals; live"  12.10525
    1 201011 "Animals; live" 12.708151
    1 201012 "Animals; live" 15.253597
    1 201101 "Animals; live"  1.042318
    1 201102 "Animals; live"  7.643689
    1 201103 "Animals; live"  5.926916
    1 201104 "Animals; live" 16.216603
    1 201105 "Animals; live"   .058009
    1 201106 "Animals; live"  7.182931
    1 201107 "Animals; live"  8.612482
    1 201108 "Animals; live"   6.82423
    1 201109 "Animals; live" 21.530921
    1 201110 "Animals; live"  6.798136
    1 201111 "Animals; live"  7.278084
    1 201112 "Animals; live" 23.301294
    1 201201 "Animals; live"   4.20473
    1 201202 "Animals; live"    .00017
    1 201203 "Animals; live"  8.978285
    1 201204 "Animals; live"    .00264
    1 201205 "Animals; live"    .00017
    1 201206 "Animals; live"  4.334437
    1 201207 "Animals; live"   .001842
    1 201208 "Animals; live" 21.741649
    1 201209 "Animals; live"  15.26398
    1 201210 "Animals; live"    .00017
    1 201211 "Animals; live"  7.764236
    1 201212 "Animals; live" 19.905743
    1 201301 "Animals; live"      .088
    1 201302 "Animals; live" 26.486636
    1 201303 "Animals; live"    .00034
    1 201304 "Animals; live"    .00017
    1 201305 "Animals; live"    .18251
    1 201306 "Animals; live"    .00017
    1 201307 "Animals; live"   9.10217
    1 201308 "Animals; live"    .00034
    1 201309 "Animals; live" 22.285176
    1 201310 "Animals; live"    .00017
    1 201311 "Animals; live"      .012
    1 201401 "Animals; live"    .00034
    1 201402 "Animals; live"     .1045
    1 201403 "Animals; live"    .00877
    1 201404 "Animals; live" 34.091872
    1 201405 "Animals; live" 27.241665
    1 201406 "Animals; live" 17.695925
    1 201407 "Animals; live"     .1155
    1 201408 "Animals; live"      .024
    1 201409 "Animals; live" 29.409172
    1 201410 "Animals; live"    .00024
    1 201501 "Animals; live"      .039
    1 201502 "Animals; live"    .04619
    1 201503 "Animals; live"    .00072
    1 201504 "Animals; live"   .319269
    1 201505 "Animals; live"  14.66878
    1 201506 "Animals; live"     .0825
    1 201507 "Animals; live" 12.737906
    1 201508 "Animals; live"      .055
    1 201509 "Animals; live"      .008
    1 201601 "Animals; live"   .304004
    1 201602 "Animals; live"  5.405002
    1 201603 "Animals; live" 15.988179
    1 201604 "Animals; live" 16.926323
    1 201605 "Animals; live"    .00048
    1 201606 "Animals; live" 10.277139
    1 201607 "Animals; live"   9.92377
    1 201608 "Animals; live"       .15
    1 201701 "Animals; live"       .09
    1 201702 "Animals; live"     .0006
    1 201703 "Animals; live"    .06025
    1 201704 "Animals; live"       .03
    1 201705 "Animals; live"   .002227
    1 201706 "Animals; live"     .0049
    1 201707 "Animals; live" 17.946324
    1 201801 "Animals; live"  3.243729
    1 201802 "Animals; live"   .003065
    1 201803 "Animals; live" 21.047769
    1 201804 "Animals; live"  11.96549
    1 201805 "Animals; live" 17.274685
    1 201806 "Animals; live"     .0047
    1 201807 "Animals; live"    .00024
    1 201901 "Animals; live"   .050237
    1 201902 "Animals; live" 14.680469
    1 201903 "Animals; live" 15.469191
    1 201904 "Animals; live" 27.951793
    1 201905 "Animals; live"  22.78883
    1 201906 "Animals; live"    .00688
    1 202001 "Animals; live"   .072152
    1 202002 "Animals; live" 20.226212
    1 202003 "Animals; live"  16.20445
    1 202004 "Animals; live" 10.160333
    1 202005 "Animals; live" 35.492134
    1 202006 "Animals; live" 19.369371
    end
    
    
    format period %tmCCYYmon
    where period is the YYYYmonth in question. I tried the code above, but it gives be a very large number, and not, for example, 2018m1. How would I do this?

  • #2
    Period is current not a datetime variable. I show the intermediate variable, but you can easily skip this step and condense this into a single variable generation step. -monhtly()- requires a delimiter between month and year, and similar steps would be necessary using other datetime functions.

    Code:
    gen periodc = substr(string(period), 1, 4) + "/" + substr(string(period), 5, 6)
    gen want = monthly(periodc, "YM")
    format want %tmCCYYmon

    Comment


    • #3
      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.

      With that said, Nick Cox has contributed the numdate command to SSC, and it provides substantial flexibility for dealing with out-of-the-ordinary presentations of dates and times.
      Code:
      . numdate monthly want = period, pattern(YM)
      
      . format want %tmCCYYmon
      
      . list period want in 1/5, clean
      
             period      want  
        1.   201001   2010jan  
        2.   201002   2010feb  
        3.   201003   2010mar  
        4.   201004   2010apr  
        5.   201005   2010may

      Comment


      • #4
        Another way to do it is via
        Code:
        ym(floor(period/100), mod(period, 100))
        Last edited by Nick Cox; 11 Sep 2022, 08:23.

        Comment


        • #5
          Thank you all so much!!

          Comment

          Working...
          X