Announcement

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

  • Convert data from DD/MM/YYYY to YYYYQQ

    I want to create a yearquarter variable from my date variable (DD/MM/YYYY)
    i.e convert "28-02-2005" to "2005Q1"

  • #2
    Code:
    help datetime
    does explain.


    Code:
    gen qdate = qofd(daily(yearquarter), "DMY"))
    format qdate %tq
    Last edited by Nick Cox; 20 Jun 2017, 06:23.

    Comment


    • #3
      An errant right parenthesis found its way into Nick's code.
      Code:
      gen qdate = qofd(daily(yearquarter, "DMY"))
      format qdate %tq

      Comment


      • #4
        William: Thanks for the fix.

        Comment


        • #5
          Hi there, in the data set I'm using, interview dates are stored as 31-Oct-2004. I would like to convert them to just the year (2004). I tried the syntax "gen int_year = year(int_date)" but got the error message "type mismatch". I understand this is because the first variable is a string and the one I am trying to generate is not. Any guidance? Thank you!

          Comment


          • #6
            Code:
            help datetime
            is where to find the guidance. In your case,

            Code:
            . di year(daily("31-Oct-2004", "DMY"))
            2004
            shows the way forward, applying daily() before you apply year().

            Comment


            • #7
              Quick follow-up question... I'm sorry I should have articulated that 31-Oct-2004 was just an example of one observation; perhaps DD-MMM-YYYY would be a better way to communicate it, with DD being numerical, MMM being letters, and YYYY being numerical. So if I'm trying to convert from DD-MMM-YYYY (string variable name: int_date) to just YYYY (numerical variable name: int_year), would that syntax be:
              di year(daily("int_date", "DMY")) int_year ?

              Thanks again...

              Comment


              • #8
                So, apply generate instead of display and insert your variable name instead of the literal date. You showed in #5 that you understood that principle, so I didn't feel I needed to explain again.

                Comment


                • #9
                  I have a related question to this topic. My data is displayed as 13jan1993 in Stata (storage type: int, display format: %td), however I want Stata to show the date as (YYYYMMDD). How can I do this?

                  Comment


                  • #10
                    You may wish to try something like:

                    Code:
                    . input str30 mydate
                    
                                                 mydate
                      1. 13jan1993
                      2. end
                    
                    . gen mydate2 = daily(mydate, "DMY")
                    
                    . format mydate2 %td
                    
                    . list
                    
                         +-----------------------+
                         |    mydate     mydate2 |
                         |-----------------------|
                      1. | 13jan1993   13jan1993 |
                         +-----------------------+
                    
                    . */ you may go on from this point
                    
                    . format %tdCCYY-NN-DD mydate2
                    
                    . list
                    
                         +------------------------+
                         |    mydate      mydate2 |
                         |------------------------|
                      1. | 13jan1993   1993-01-13 |
                         +------------------------+
                    I wouldn't format dates as a whole sequence of numbers, because it may be confusing to read, but if you wish to produce such a thing, here it goes:

                    Code:
                    . gen mydate3 = mydate2
                    
                    . format %tdCCYYNNDD mydate3
                    
                    . list
                    
                         +-----------------------------------+
                         |    mydate      mydate2    mydate3 |
                         |-----------------------------------|
                      1. | 13jan1993   1993-01-13   19930113 |
                         +-----------------------------------+
                    Hopefully that helps.
                    Last edited by Marcos Almeida; 24 May 2019, 05:22.
                    Best regards,

                    Marcos

                    Comment

                    Working...
                    X