Announcement

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

  • Extracting week and year information from a date variable in the format %tCMonth_dd,_CCYY_HH:MM

    My date is in the format %tCMonth_dd,_CCYY_HH:MM

    Yet, the "week" and "year" commands are not recognized, and return null (.) values.

    The code I am trying to run is:

    gen week = week(wdatetime)
    gen year = year(wdatetime)

    I have tried converting the date variable to %td, %tc etc. but with no luck
    Attached Files

  • #2
    That variable is not a date variable, it is a date-time variable, or, in Stata-speak, a Clock variable. The -year()- and -week()- functions only work correctly with date variables. And changing the display format to %td does not convert the actual variable--it just makes it look different in outputs. What you need is:

    Code:
    gen week = week(dofC(wdatetime))
    gen year = year(dofC(wdatetime))
    leveraging the -dofC()- function which actually converts Clock variables to dates (as opposed to just changing how they look).

    I appreciate your effort to show example data, but attachments are discouraged here. Some of us, myself included, will not download files from people we do not know. The most useful way to show example data, which is simpler and faster for both you and the reader, is to use the -dataex- command. 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.

    Added: Are you aware that Stata's "week" is defined by: Week 1 always begins on January 1. Week 52 has 8 or 9 days, depending on whether it is a leap year. This definition of week does not play well with the more common sense of week as a block of 7 consecutives days beginning on Sunday (or Monday in some places). In truth, there is really no good way to define a week. Just be clear that you are relying on Stata's approach, and that it may differ from what others use. (In particular, if you plan to combine your data set with some other data set that comes with "weekly" data observations, it behooves you to find out what definition of week was used for creating the other data set. If it isn't the same one Stata uses, you are in for a world of hurt!)
    Last edited by Clyde Schechter; 11 Apr 2024, 17:31.

    Comment


    • #3
      Thank you, Clyde! Your code and all of your helpful advice (incl. re: attachments) are noted. All too familiar with the competing definitions of "week".

      Comment

      Working...
      X