Announcement

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

  • converting dates (year and month)

    Dear All, Is there a more concise way to go from date to newdate below? Thanks.
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str6 date float newdate
    "202101" 732
    "202102" 733
    end
    format %tm newdate
    Ho-Chuan (River) Huang
    Stata 19.0, MP(4)

  • #2
    I found that we can ssc install numdate (by Nick Cox), and
    Code:
    numdate monthly d = date, pattern(YM)
    Ho-Chuan (River) Huang
    Stata 19.0, MP(4)

    Comment


    • #3
      This problem is often discussed here and is the focus of a dedicated Tip in the Stata Journal visible at https://www.stata-journal.com/articl...article=dm0096

      In essence, monthly() is not quite smart enough. Here are two work-arounds.

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input str6 date float newdate
      "202101" 732
      "202102" 733
      end
      format %tm newdate
      
      gen wanted1 = monthly(substr(date, 1, 4) + " " + substr(date, 5, 2), "YM")
      
      gen wanted2 = ym(floor(real(date)/100), mod(real(date), 100))
      
      format wanted? %tm
      Last edited by Nick Cox; 31 Oct 2021, 05:59.

      Comment


      • #4
        Dear Nick, Thanks for these suggestions. I will go with the second method.
        Ho-Chuan (River) Huang
        Stata 19.0, MP(4)

        Comment

        Working...
        X