Announcement

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

  • Month Week Number

    If i have a date variable, is there a way of knowing with week number of the month(i.e 1-4) is it?
    I am using week() but it can only give the week number of the year(i.e 1-52).
    Here is a sample code:


    clear all
    set obs 1

    gen mystr=c(current_date)
    gen eventdate = date(mystr, "DMY")
    format eventdate %td
    gen b = week(eventdate)


  • #2
    Well, there is no built-in function that I am aware of to do that. In turn, that may be because it isn't clear how week of the month is defined. One definition would be that days 1-7 are week 1, 8-14 are week 2, 15-21 are week 3, 22-28 are week 4, and 29-31 (if applicable) are week 5. That would be fairly simple to program:

    Code:
    gen int day_in_month = eventdate - dofm(mofd(eventdate)) + 1
    gen int week_in_month = ceil(day_in_month/7)
    If that's not what you have in mind for week in the month, please provide a specific definition, and perhaps we can figure it out.

    Comment


    • #3
      Note also that day() yields day of the month,

      Code:
      . di day(mdy(1, 10, 2018))
      10

      so Clyde's code is equivalent to

      Code:
      gen int week_in_month = ceil(day(eventdate)/7)
      The easiest way to handle weeks, in my view, is as daily dates spaced 7 days apart. Weeks can be defined by the daily date at the start, or the end, or in any other convenient way.

      Comment

      Working...
      X