Announcement

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

  • Converted datasets to weekly

    Dear all

    I have quarterly longitudinal data sets (8 consecutive quarters). I want to convert this data from quarterly to weekly.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double PERSID byte(quarter WEEK Inds07m)
    10493040101 3  4 -9
    10493040101 4  4 -9
    10493040101 5  4 -9
    10493040101 6  4 -9
    10493040101 7  4 -9
    10694020101 4  6 -9
    10694020101 5  6 -9
    10694020101 6  6 -9
    10694020101 7  6 -9
    10694020101 8  6 -9
    10694020102 4  6 -9
    10694020102 5  6 -9
    10694020102 6  6 -9
    10694020102 7  6 -9
    10694020102 8  6 -9
    10792030101 2  7 -9
    10792030101 3  7 -9
    10792030101 4  7 -9
    10792030101 5  7 -9
    10792030101 6  7 -9
    10793010101 3  7 15
    10793010101 4  7 15
    10793010101 5  7 15
    10793010101 6  7 15
    10793010101 7  7 15
    10794010101 4  7  9
    10794010101 5  7  9
    10794010101 6  7  9
    10794010101 7  7  9
    10794010101 8  7  9
    10794010102 4  7  9
    10794010102 5  7  9
    10794010102 6  7  9
    10794010102 7  7  9
    10794010102 8  7  9
    10993020101 3  9 16
    10993020101 4  9 16
    10993020101 5  9 16
    10993020101 6  9 16
    10993020101 7  9 -9
    10993020102 3  9 15
    10993020102 4  9 15
    10993020102 5  9 15
    10993020102 6  9 15
    10993020102 7  9 15
    11091010101 1 10 16
    11091010101 2 10 16
    11091010101 3 10 16
    11091010101 4 10 16
    11091010101 5 10 16
    11093030101 3 10 -9
    11093030101 4 10 -9
    11093030101 5 10 -9
    11093030101 6 10 -9
    11093030101 7 10 -9
    11094010101 4 10 15
    11094010101 5 10 -9
    11094010101 6 10 -9
    11094010101 7 10 -9
    11094010101 8 10 -9
    11094010102 4 10 17
    11094010102 5 10 17
    11094010102 6 10 17
    11094010102 7 10 17
    11094010102 8 10 17
    11291020101 1 12 -9
    11291020101 2 12 -9
    11291020101 3 12 -9
    11291020101 4 12 -9
    11291020101 5 12 -9
    11292020101 2 12  7
    11292020101 3 12  7
    11292020101 4 12  7
    11292020101 5 12  7
    11292020101 6 12  7
    11294030101 4 12 15
    11294030101 5 12 15
    11294030101 6 12 15
    11294030101 7 12 15
    11294030101 8 12 15
    11294030102 4 12 -9
    11294030102 5 12 -9
    11294030102 6 12 -9
    11294030102 7 12 -9
    11294030102 8 12 -9
    20191020101 1  1 16
    20191020101 2  1 16
    20191020101 3  1 16
    20191020101 4  1 16
    20191020101 5  1 16
    20191020102 1  1  7
    20191020102 2  1  7
    20191020102 3  1  7
    20191020102 4  1  7
    20191020102 5  1  7
    20191040101 1  1 19
    20191040101 2  1 19
    20191040101 3  1 19
    20191040101 4  1 19
    20191040101 5  1 19
    end
    label values quarter quarter
    label def quarter 1 "Jan-Mar 2019", modify
    label def quarter 2 "April-June 2019", modify
    label def quarter 3 "July-Sep 2019", modify
    label def quarter 4 "Oct-Des 2019", modify
    label def quarter 5 "Jan-Mar 2020", modify
    label def quarter 6 "April-June 2020", modify
    label def quarter 7 "July-Sep 2020", modify
    label def quarter 8 "Oct-Des 2020", modify
    label values WEEK WEEK
    label values Inds07m Inds07m5
    label def Inds07m5 -9 "Does not apply", modify
    label def Inds07m5 7 "Wholesale And Retail Trade; Repair Of Motor Vehicles And Motorcycles", modify
    label def Inds07m5 9 "Accommodation And Food Service Activities", modify
    label def Inds07m5 15 "Public admin and defence", modify
    label def Inds07m5 16 "Education", modify
    label def Inds07m5 17 "Human Health And Social Work Activities", modify
    label def Inds07m5 19 "Other service activities", modify

    I have the variable "WEEK" in my data. There are 13 weeks in each quarter, and this variable identifies which week in the quarter the interview took place.

    I want to convert it to a weekly basis so that the variable "week" represents the week, month and year. Could we do it this way?

    Note that the numbers of weeks are repetitive in all quarters.

  • #2
    This might work, depending on how weeks are defined. I believe I have, in an earlier thread of yours, explained that there are different definitions of week in use, and these different definitions would lead to different results. Here I am applying the definition of week that Stata uses. Specifically week 1 of any given year begins on January 1, regardless of what day of the week that falls on. A new week begins every 7 days thereafter, except that at the end of the year, the 52nd week will be longer than 7 days and include however many additional days are needed to reach through December 31 (this number will vary depending on whether it is a leap year). In particular, there is no abbreviated 53rd week.

    Added: Notice that under this definition of week, some weeks will overlap two quarters. For example, the last day of 2019Q1, 31 Mar 2019, and the first day of 2019Q2, 1 Apr 2019, are both in the 13th week of year 2019. Because the duration of a year is never a multiple of 7 days, and that of a quarter is only sometimes so, anomalies like this arise in any attempt to frame larger units of time into weeks. That is, although the different ways weeks can be defined will give rise to different anomalies, it is mathematically inevitable that any definition of week leads to some kind of anomaly.

    Code:
    gen int qdate = quarter + tq(2018q4)
    format qdate %tq
    gen wanted = WEEK + wofd(dofq(qdate)) - 1
    format wanted %tw
    Last edited by Clyde Schechter; 21 Mar 2023, 17:50.

    Comment


    • #3
      Somehow the definition you're using implies that 365 or 366 days in a year are always mapped to 13 weeks per quarter.

      That should allow you to define your own numbering as a composite of quarter and week.

      If this definition is as strict as implied, you can and should keep clear of Stata's own weekly functions and formats.

      Code:
      egen wdate = group(quarter week), label 
      Thus for once my advice differs from @Clyde Schechter's.

      It's odd that the data example alludes to value labels for WEEK, but they aren't defined.

      Comment

      Working...
      X