I want to convert dates as weeks, but I end up getting the wrong week in some of the options. I need to use weeks as previous analyses and subgroups are based on week numbers. I am using Stata 17 for Windows. Below is a dummy example. As data are confidential, I cannot use -dataex-.
The substantive problem is this: Patients are recruited at "ddate". They receive treatments throughout the study period that are not scheduled at set dates (treat). I've identified the day of the week to make the problem easier to understand. Generating week number for the dday (dweek) and treat (tweek),some of them are incorrect (dweek should be 5, not six; only row 5 is correct in tweek).
So I tried a different angle. ddate is week 1 for each respective patient. I have found a code in a different question here that could give me the treatment week number when using ddate as week 1, and each consecutive week number having 7 days.
However, the problem is still present - I believe the reason this time is that it adds 7 days from ddate, and not from the last day in the week ddate is. I believe that making a new variable where ddate is the closest Monday (questionnaires were filled out on Mondays, so the week needs to start on a Monday). But my skills don't include that, unfortunately. So far I've not found anyone asking for what I need. Personally I consider this a rather complex issue and would greatly appreciate if someone could help me find a solution for my problem, a hint in the right direction, or a link where I myself can find the answer.
The substantive problem is this: Patients are recruited at "ddate". They receive treatments throughout the study period that are not scheduled at set dates (treat). I've identified the day of the week to make the problem easier to understand. Generating week number for the dday (dweek) and treat (tweek),some of them are incorrect (dweek should be 5, not six; only row 5 is correct in tweek).
Code:
clear *ddate=date recruited *treat= date of each treatment input str9(ddate treat) "2/5/2021" "2/5/2021" "2/5/2021" "2/7/2021" "2/5/2021" "2/10/2021" "2/5/2021" "2/17/2021" "2/5/2021" "3/23/2021" end *making date into day number gen int dday = date(ddate, "MDY") gen int treatday = date(treat, "MDY") *identifying which day of week the day is *day recruited: gen byte dname = dow(dday) forval i=1/7 { label def dname `=`i'-1' `"`: word `i' of `c(Weekdays)''"', modify } label values dname dname *days of treatment gen byte tname = dow(treatday) forval i=1/7 { label def tname `=`i'-1' `"`: word `i' of `c(Weekdays)''"', modify } label values tname tname *generate week number for recruitment day and treatment day gen dweek = week(dday) gen tweek = week(treatday) list, sep(7) +------------------------------------------------------------------------------+ | ddate treat dday treatday dname tname dweek tweek | |------------------------------------------------------------------------------| 1. | 2/5/2021 2/5/2021 22316 22316 Friday Friday 6 6 | 2. | 2/5/2021 2/7/2021 22316 22318 Friday Sunday 6 6 | 3. | 2/5/2021 2/10/2021 22316 22321 Friday Wednesday 6 6 | 4. | 2/5/2021 2/17/2021 22316 22328 Friday Wednesday 6 7 | 5. | 2/5/2021 3/23/2021 22316 22362 Friday Tuesday 6 12 | +------------------------------------------------------------------------------+
Code:
*using ddate as basis for week number - i.e. ddate = first week gen int alt_tweek = floor(( treatday - dday )/7) + 1 list, sep(7) +-----------------------------------------------------------------------------------------+ | ddate treat dday treatday dname tname dweek tweek alt_tw~k | |-----------------------------------------------------------------------------------------| 1. | 2/5/2021 2/5/2021 22316 22316 Friday Friday 6 6 1 | 2. | 2/5/2021 2/7/2021 22316 22318 Friday Sunday 6 6 1 | 3. | 2/5/2021 2/10/2021 22316 22321 Friday Wednesday 6 6 1 | 4. | 2/5/2021 2/17/2021 22316 22328 Friday Wednesday 6 7 2 | 5. | 2/5/2021 3/23/2021 22316 22362 Friday Tuesday 6 12 7 | +-----------------------------------------------------------------------------------------+
Comment