help for myweeks

Numbered weeks from daily dates

myweeks
dailydatevar
[if] [in]
,
generate(newvar)
[
dowstart(#)
dailydate
format(format)
]

Description

myweeks takes a numeric daily date variable assumed to follow Stata's
rule that daily dates are integers counted from 0 at 1 January 1960 and
generates a new variable numbering the distinct weeks in which they occur
with distinct integers.

myweeks uses the following rules.

1. Each week starts on a particular day of the week, one of Sunday to
Saturday.

2. By default weeks start on Sundays, for which the Stata function
dow() returns 0. You may choose a different day to start the
week by specifying dowstart() with an integer between 1 (meaning
Monday) and 6 (meaning Saturday).

3. By default myweeks uses a rule broadly similar to those used in
official Stata: the first complete week within 1960 using the rules so
far is numbered 0. This is best explained by example. 1 January 1960,
daily date 0, was a Friday for which dow() returns 5. So 2 January
1960 was a Saturday and 3 January 1960 was a Sunday. Hence week 0 starts
on 3 January 1960 if weeks start on day of the week 0, meaning Sunday.
Similarly week 0 starts on 4, 5, 6, 7 January if weeks start on days of
the week 1, 2, 3, 4, but on 1, 2 January if weeks start in days of the
week 5, 6. Any previous weeks in the data are indexed with negative
integers. Any later weeks are indexed with positive integers.

4. Optionally you may specify using the dailydate option that
weeks as defined here are indexed by the daily dates that start them.
Such week indexes differ by multiples of 7. Users proceeding to
tsset or xtset will often find it essential in due course to
specify the option delta(7).

Remarks

Dates in Stata are complicated because dates are complicated. You may
have been familiar long since with many of the rules governing the
calendar, but that should not lead you to suppose that working with
dates will always be simple. There are many different date
conventions and Stata may need to be told about yours. Weeks are
especially complicated because several definitions are in use,
implying different code both for identifying weeks and for working with
weekly data for weekly dates or for any other dates.

Stata's official functionality for weeks hinges on an idiosyncratic
definition: week 1 of any year always starts on 1 January in that
year and subsequent weeks in the same year start at multiples of 7 days
later, except that week 52 is always 8 or 9 days long. No week 53 is
ever observed. This definition is documented, but many users have not
read the documentation or failed to think through its consequences.
Stata's definition ensures that weeks always nest within years, but it
is not consistent with any other definition of week. See, for example,
Cox (2010, 2012a, 2012b, 2019, 2022a, 2022b) on a variety of related
challenges. That is more references than you may want to be given.
Cox (2010) is most relevant, but not essential, for understanding
the main idea here.

Warning: The whole point of myweeks is to step outside
Stata's official functionality for weeks and to offer an alternative. It
is consistent only with Stata's dow() function and whatever is
consistent with that. It neither assumes nor can be used successfully in
conjunction with Stata's own weekly dates; nor with functions such as
yw(),
weekly(),
week(),
dofw() or
wofd(); nor with Stata's display formats for weekly dates.
Weeks as numbered by myweeks will often span years, starting in
one year and ending in the next.

myweeks is often redundant. Quite commonly, weekly data arrive
indexed by a daily date variable with values 7 days apart. Such dates
may be simply understood and used without needing to be converted to any
other form. In particular, they lend themselves to easily understood
graphics or tabulations. Crucially, however, the time step of 7 days
may need to be explained, as already mentioned.

myweeks does not assume completeness of dates in any sense. You
may have daily dates only for certain days of the week, say weekdays
Mondays to Fridays. You may lack data for public holidays. You may have
gaps in your dataset in which certain weeks are absent and not
represented by any daily dates. The command will not be fazed by such
incomplete data.

A preference for defining a week by the day of the week on which it ends can
be met with two simple twists. For example, weeks defined to end on Fridays
necessarily start on Saturdays and each ending Friday is 6 days later than the
starting Saturday. Otherwise see Cox (2010). This point is developed
in the code examples.

You may seek a numbering scheme that differs from what is provided by
myweeks. Other than writing your own code, that is best achieved
by following this command with summarize and adjusting your
numbering using the minimum shown, accessible as r(min). No such
option is provided here to emphasize that any such variation is your
responsibility. At the risk of stating the obvious: If you are using
different datasets that you will eventually append or
merge, it is best to change to your own numbering scheme only when all
datasets have been combined. If you use myweeks consistently,
datasets can be combined confidently. In addition, be wary of
egen's group() function: although it helpfully maps a sequence
of increasing values to integers 1 up, it will ignore gaps in any sequence.

Users sometimes want to combine weeks into longer periods, lasting say 2,
3 or 4 weeks. For such aggregation, my best advice is to use
floor() or ceil(), say as discussed in Cox (2018).

Once you have classified daily dates into weeks, you may wish to proceed
with some kind of reduction using that framework, either with or without
some other classification, say into panel or longitudinal data. In
addition to official commands such as collapse, contract,
and table, rangestat and rangerun from SSC address
certain fairly common needs.

Options

generate() specifies a new variable name to hold week numbers.
This is a required option.

dowstart() specifies a day of the week on which weeks are deemed
to start. The syntax is consistent with that of official function
dow() and may specify any integer between 0 for Sunday and 6 for
Saturday. The defult is 0.

dailydate specifies that weeks should be specified by the daily
date of the first day of the week. It is not a problem if such a daily
date is not represented in the data.

format() specifies a format for the daily date variable requested
with the previous option. The default is %td. The only check on any
format specified is that it works with integers. You are warned against the
fallacy that specifying a date format other than a daily date format is
a way to convert one kind of date to another: see Cox (2012c).

Examples

. clear

. set obs 42

. * sandbox: first 21 days of 1960; 21 days centred on current date

. gen date = mdy(1, _n, 1960) in 1/21

. replace date = daily("$S_DATE", "DMY") + (_n - 32) in 22/42

. format date %td

. gen dow = dow(date)

. gen which = _n > 21

. * week numbers starting each day of the week

. forval d=0/6 {

. myweeks date, gen(w`d') dowstart(`d')

. myweeks date, gen(W`d') dowstart(`d') daily

. local show `show' w`d' W`d'

. }

. list date dow `show', sepby(which w0)

. * weeks ending Friday

. myweeks date, gen(wef) dowstart(6)

. myweeks date, gen(WEF) dowstart(6) dailydate

. replace WEF = WEF + 6

. list date dow wef WEF, sepby(which wef)

Author

Nicholas J. Cox, Durham University

n.j.cox@durham.ac.uk

References

Cox, N.J. 2010. Stata tip 68: Week assumptions.
Stata Journal 10(4): 682-685.

Cox, N.J. 2012a. Stata tip 111: More on working with weeks.
Stata Journal 12(3): 565-569.

Cox, N.J. 2012b. Stata tip 111: More on working with weeks, erratum.
Stata Journal 12(4): 765.

Cox, N.J. 2012c. Stata tip 113: Changing a variable's format:
What it does and does not mean.
Stata Journal 12(4): 761-764.

Cox, N.J. 2018. Speaking Stata: From rounding to binning.
Stata Journal 18(3): 741-754.

Cox, N.J. 2019. Speaking Stata: The last day of the month.
Stata Journal 19(3): 719-728.

Cox, N.J. 2022a. Stata tip 145: Numbering weeks within months.
Stata Journal 22(1): 224-230.

Cox, N.J. 2022b. Erratum: Stata tip 145: Numbering weeks within months.
Stata Journal 22(2): 465-466.

Also see

Online: help for datetime,

datetime functions,

datetime display formats,

tsset,

xtset