Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

# Re: st: Generating a new date variable from another date variable

 From Nick Cox To statalist@hsphsun2.harvard.edu Subject Re: st: Generating a new date variable from another date variable Date Mon, 4 Mar 2013 10:29:30 +0000

```Joseph Coveney has expanded in detail on my suggestion. Here is
another way to think about it.

1. If a week ends on Saturday, it starts on the previous Sunday.
Either way, we can classify weeks by the days that start or end them.

2. Sunday starts are a little easier to deal with because -dow()-
evaluates to 0.

3. The mapping from daily date to the previous Sunday is just

. gen WeekStart = DailyDate - dow(DailyDate)

and this works for the Sunday itself as we are just subtracting 0.

4. It is then simply

. gen WeekEnd = WeekStart + 6

5. You can combine the two in one statement.

SJ-12-3 dm0065  . . . . . . . . . .  Stata tip 111: More on working with weeks
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
Q3/12   SJ 12(3):565--569                                (no commands)
discusses how to convert data presented in yearly and weekly
form to daily dates and how to aggregate such data to months
or longer intervals

SJ-10-4 dm0052  . . . . . . . . . . . . . . . . Stata tip 68: Week assumptions
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
Q4/10   SJ 10(4):682--685                                (no commands)
tip on Stata's solution for weeks and on how to set up
your own alternatives given different definitions of the
week

On Sat, Mar 2, 2013 at 7:37 PM, Pinaki Mitra <mitrapin@gmail.com> wrote:
> I need to be able to keep all observations. All I need to do is to
> assign "Saturday" for variable weekend for all observations in any
> given week. So, for the 1st week of 2006, observations for DailyDate
> with values 01/01/06, 01/02/06, 01/03/06, 01/04/06, 01/05/06,
> 01/06/06, 01/07/06 will have a value of 01/07/06 (which is Satusday)
> for the variable weekend. It will look like:
>
> DailyDate(original variable) WeekendDate (Want to generate)
> 01/01/2006 01/07/2006
> 01/03/2006 01/07/2006
> 01/04/2006 01/07/2006
> 01/05/2006 01/07/2006
> 01/06/2006 01/07/2006
> 01/07/2006 01/07/2006
>
> I am not sure how to do this with dow() function.
>
> On Sat, Mar 2, 2013 at 11:14 AM, Nick Cox <njcoxstata@gmail.com> wrote:
>> There is no need for any kind of loop here. Use the -dow()- function and
>> check for the days of the week you do or don't want.

On 2 Mar 2013, at 15:14, Pinaki Mitra <mitrapin@gmail.com> wrote:

>>> I have a data variable where each observation is different dates of
>>> 2006 to 2012. I need to summarize this data by weekend (each Saturday
>>> of each week). I attempted to generate a new date variable (weekend)
>>> and collapse the data by weekend. But I am having difficulty in
>>> generating the weekend variable from the actual date variable.
>>> For example,
>>> DailyDate(original variable) WeekendDate (Want to generate)
>>> 01/01/2006 01/07/2006
>>> 01/03/2006 01/07/2006
>>> 01/04/2006 01/07/2006
>>> 01/05/2006 01/07/2006
>>> 01/06/2006 01/07/2006
>>> I tried:
>>> local stdate1:di %dN/D/Y (date("01/01/2006","MDY"))
>>> local enddate1:di %dN/D/Y (date("12/31/2012","MDY"))
>>> gen WndDate=date("01/01/2006","MDY"); format WndDate %dN/D/Y;
>>> while `stdate1'<=`enddate1'{;
>>> replace WndDate=`stdate1'+6 if DailyDate>=`stdate1' &
>>> DailyDate<=`stdate1'+6;
>>> local stdate1=`stdate1'+6;
>>> };
*
*   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/
```