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

  • a basic monthly date question

    Hi all
    This is a basic question but I can not get the correct code to work.

    I have a variable (datadate) that is in a form similar to 31 Mar 78
    I want to create monthly variable and then sort my data monthly.

    I used to do this code for quarterly data:
    gen fqdate = qofd(datadate)
    format fqdate %tq
    What would be the counterpart for monthly data?


  • #2
    The section of the help datetime output that discusses SIF-to-SIF conversion and includes the qofd() function tells us that the counterpart for monthly data is mofd().

    But with that said, if your variable datadate looks as you say it does, I suspect it may be stored as a string rather than as a Stata date. In that case, you will first want to create a Stata daily date variable. Putting this all together,
    . generate str16 datadate = "31 Mar 78"
    . generate date_d = daily(datadate,"DM19Y")
    . format date_d %td
    . generate date_m = mofd(date_d)
    . format date_m %tm
    . list, clean noobs
         datadate      date_d   date_m  
        31 Mar 78   31mar1978   1978m3


    • #3
      William gives excellent advice.

      You might also be interested in downloading Nick Cox's new program -numdate- from SSC. It handles many of these tasks in a way that taxes one's memory and understanding of Stata dates less than working directly with the built-in functions.


      • #4
        Thanks a lot. I will try to use both and provide feedback.
        Many thanks


        • #5
          I suspect that numdate (SSC) is not flexible enough to convert date types on the fly. It expects minimal input, not extra elements to be ignored. Hence follow William at #2.