Announcement

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

  • Extracting day and week from date

    Hi. I have a date variable named "date" in a string format. I converted it to the date format (variable "daily_date") with the code below. I am trying to create a variable that indicates the week number and the day of the week, i.e. Monday=1, Tuesday=2, Wednesday=3. However, my code below for generating the week number is generating the wrong week number. Any help in generating the week number and day of the week would be much appreicated.


    Code:
    input str9 date float(daily_date monthly_date weekly_date)
    
    "1/1/2021"  22281 1 1
    "1/10/2021" 22290 1 2
    "1/10/2021" 22290 1 2
    
    //converting string to the date format
    gen daily_date = date(date , "MDY")
    format daily_date %td
    
    //gen week number
    gen weekly_date = week(daily_date)

  • #2
    It's worth spending some time reading through the functions available in -help datetime functions-. There's a lot there and they will often save you a lot of effort.

    Generating the day of the week is straightforward enough using the -dow()- function, keeping in mind that Stata begins counting Sunday through Saturday as 0 through 6. Simple arithmetic can be used to change the starting day of the week if that's necessary.

    Counting weeks could be a little less straightforward, depending on your calendar. The -week()- function begins counting from the first 7-day period of the start of the calendar year, so you get weeks 1 through 52. If this is not what you wanted, then you need to explain your calendar system.

    Code:
    input str9 date
    "1/1/2021"
    "1/2/2021"
    "1/3/2021"
    "1/4/2021"
    "1/5/2021"
    "1/6/2021"
    "1/7/2021"
    "1/8/2021"
    "1/9/2021"
    "1/10/2021"
    "1/28/2021"
    end
    
    //converting string to the date format
    gen int daily_date = date(date , "MDY")
    format daily_date %td
    
    gen byte weekday = dow(daily_date)
    forval i=1/7 {
        label def weekday `=`i'-1' `"`: word `i' of `c(Weekdays)''"', modify
    }
    label values weekday weekday
    
    gen week_num = week(daily_date)
    Result

    Code:
      +-----------------------------------------------+
      |      date   daily_date     weekday   week_num |
      |-----------------------------------------------|
      |  1/1/2021    01jan2021      Friday          1 |
      |  1/2/2021    02jan2021    Saturday          1 |
      |  1/3/2021    03jan2021      Sunday          1 |
      |  1/4/2021    04jan2021      Monday          1 |
      |  1/5/2021    05jan2021     Tuesday          1 |
      |  1/6/2021    06jan2021   Wednesday          1 |
      |  1/7/2021    07jan2021    Thursday          1 |
      |  1/8/2021    08jan2021      Friday          2 |
      |  1/9/2021    09jan2021    Saturday          2 |
      | 1/10/2021    10jan2021      Sunday          2 |
      | 1/28/2021    28jan2021    Thursday          4 |
      +-----------------------------------------------+

    Comment


    • #3

      Leonardo Guizzetti is bang on. In addition,

      Code:
      search epiweek 
      
      search week, sj

      Comment


      • #4
        Thank you Leonardo Guizzetti and Nick Cox. This was very helpful and taught me a lot.

        Comment

        Working...
        X