Announcement

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

  • Generating quarterly date from string

    Dear Community,

    I struggle with obtaining date from a string like 05.2012.
    I would like to have quarterly data like 2012q2
    My learning curve is extremely slow. I know I cannot destring. I know how to obtain year from it. However, still struggle with getting what I want

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str7 var1
    "05.2012"
    end
    
    gen wanted = qofd(dofm(monthly(var1, "MY")))
    format wanted %tq
    In the future, when showing data examples, please use the -dataex- command to do so, as I have here. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Added: Stata does not have any functions that directly translate monthly dates to quarterly. But, all of the date variable types can be translated directly to or from the daily date variable type. So the general approach here is to take what you have and use a -dofX()- function to make a daily date out of it, and then use a -Xofd()- function to convert that to what you want.
    Last edited by Clyde Schechter; 25 Apr 2022, 10:54.

    Comment


    • #3
      This is a variation on your previous thread https://www.statalist.org/forums/for...g-into-integer

      There is only one key difference between people who can solve these problems and those who can't. The first group has read

      Code:
      help datetime
      often many times.

      The thread cited explained how to get a monthly date variable from a string. Then you need to convert to a quarterly date. For string constants we can see what to do using display

      Code:
      . di %tq qofd(dofm(monthly("05.2012", "MY")))
      2012q2
      The steps are

      1. Extract a monthly date -- here with monthly()

      2. Convert to a quarterly date -- here with qofd(dofm())

      3. Assign an appropriate display format.

      If data are held in variables, you can do things like

      Code:
      . gen test = "05.2012"
      
      . gen qdate = qofd(dofm(monthly(test, "%MY"))) 
      
      . format qdate %tq
      Naturally, you don't need to create test as (presumably) you already have the string date in a variable with a name you know, but didn't tell us.

      There is an alternative approach using commands from the numdate package on SSC.

      Code:
      . numdate monthly mdate = test, pattern(MY)
      
      . convdate quarterly qdate = mdate
      
      . l test mdate qdate in 1
      
           +---------------------------+
           |    test    mdate    qdate |
           |---------------------------|
        1. | 05.2012   2012m5   2012q2 |
           +---------------------------+
      That's not really different, but it does some of the work for you. More at https://www.statalist.org/forums/for...date-variables

      Comment


      • #4
        "%MY" in #3 is a typo for "MY".

        Comment


        • #5
          Thank you all for your help. I'm always asking for it when got stuck in finding the solution. The last thing I want is to take advantage of your kindness.
          Last edited by sladmin; 28 Aug 2023, 09:38. Reason: anonymize original poster

          Comment

          Working...
          X