Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: generate a variable for Project Week

 From Nick Cox To "statalist@hsphsun2.harvard.edu" Subject Re: st: generate a variable for Project Week Date Mon, 27 May 2013 18:44:28 +0100

```The project starts on May 6. That being so, weeks can be numbered

gen week = ceil((1 + date - mdy(5,6,2013))/7)

The logic here is that

date - mdy(5,6,2013)

is 0 .. 6 in the first week, 7..13 in the second week, etc.

and so

1 + date - mdy(5,6,2013)

is 1 .. 7 in the first week, 8..14 in the second week, etc.

and so dividing by 7 gives

1/7 .. 7/7 in the first week, 8/7 .. 14/7 in the second week, etc.

and so -ceil()- applied to that gives 1 in the first week, 2 in the
second week, etc.

The logic here is self-contained, but for more on handling weeks in general, see

Stata tip 111: More on working with weeks, erratum. Stata Journal 12(4)

Stata tip 111: More on working with weeks Stata Journal  12(3)

Stata tip 68: Week assumptions. Stata Journal 10(4)

Nick
njcoxstata@gmail.com

On 27 May 2013 18:29, Michael McCulloch <mm@pinestreetfoundation.org> wrote:

> To generate a variable for elapsed Project Week, I have written the following manual commands, in which the first Project Week is May 6, 2013 through May 12, the second week is May 13 through the 19th, and on forward:
>
> gen week=1 if date>=mdy(5,6,2013) & date<mdy(5,12,2013)
> replace week=2 if date>=mdy(5,13,2013) & date<mdy(5,19,2013)
>
> Is there a shorter more elegant way to accomplish this?
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/
```