Announcement

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

  • Generating ith date of year variable from day/month/year date variable?

    Hi all,

    I am wondering if it is possible to generate a variable in the format of ith day of the year from a date variable. I have been reading about working with date variables in Stata, but have yet to find a solution to this problem.

    So, using a small subset of the data for an example, I have:

    date
    07oct2003
    01apr2005
    07oct2003
    30oct2003
    01apr2005
    01apr2005
    29dec2003
    30oct2003
    30oct2003
    01apr2005


    And I am hoping to create a variable with values of:

    day_of_year
    280
    91
    280
    303
    91
    91
    363
    303
    303
    91


    I understand that this is complicated by leap years (2000, 2004, etc.), but I am hoping that Stata can automatically account for that somehow...
    Any advice you might be able to offer would be greatly appreciated!

    Best wishes,
    Matt

  • #2
    Code:
    di doy(`=td(07oct2003)')

    Comment


    • #3
      If your date variable is already a Stata internal format date variable, then it is very easy:
      Code:
      gen wanted = doy(date)
      If it is a string variable that looks like a date to human eyes, then you have to first make a Stata internal date variable out of it:
      Code:
      gen date_sif = dailiy(date, "DMY")
      assert missing(date_sif) == missing(date)
      format date_sif %td
      gen wanted = doy(date_sif)
      Note: I believe the -doy()- function was introduced in version 17. If you are using a Stata version older than that, you are supposed to say so when you post so that neither you nor I waste our time with code that you cannot actually run. I'm assuming you have version 17 or 18. If you don't, try this code, as I may be mistaken about when it came into Stata. If Stata says it doesn't recognize it, post back.

      In the future, so that those who want to help you don't have to guess what kinds of variables you are working with, please show example data and use the -dataex- command to do so. If you are running version 18, 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.

      When asking for help with code, always show example data. When showing example data, always use -dataex-.

      Added: Crossed with #2.

      Comment


      • #4
        Apologies, I will be sure to use -dataex- in the future.

        Thanks so much for the help!

        Comment


        • #5
          Apologies, I will be sure to use -dataex- in the future.

          Thanks so much for the help!

          Comment


          • #6
            doy() has been in Stata for some time, I would guess since at least Stata 10;

            Comment

            Working...
            X