create 1st Monday in July

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

create 1st Monday in July

wsu_wright
I have a dataset that daily collects data on student admits that spans
several years, for each record there is a date variable (mm/dd/yyyy).  I
need to be able to extract data & identify the 1st Monday of each week,
each year must start on the 1st Monday of July.  So I'm looking for
syntax that would allow me to have a week counter (1-52) in which 1
always starts on the 1st Monday of July across all the years.  I suspect
the xdate function would work, I could extract month & week but I'm not
certain how to associated the week num with the 1st week in July since
the xdate weeknum would not always be the same for the 1st Monday.
Perhaps xdate's wkday would work by capturing the 1st wkday value of 2
(monday) for month 7.

Thanks in advance,

David

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: create 1st Monday in July

Bruce Weaver
Administrator
David Wright-6 wrote
I have a dataset that daily collects data on student admits that spans
several years, for each record there is a date variable (mm/dd/yyyy).  I
need to be able to extract data & identify the 1st Monday of each week,
each year must start on the 1st Monday of July.  So I'm looking for
syntax that would allow me to have a week counter (1-52) in which 1
always starts on the 1st Monday of July across all the years.  I suspect
the xdate function would work, I could extract month & week but I'm not
certain how to associated the week num with the 1st week in July since
the xdate weeknum would not always be the same for the 1st Monday.
Perhaps xdate's wkday would work by capturing the 1st wkday value of 2
(monday) for month 7.

Thanks in advance,

David
How about something like this?

data list free / datevar (date11).
begin data
15-Jul-2009 15-Jul-2010
15-Aug-2009 15-Aug-2010
15-Sep-2009 15-Sep-2010
15-Oct-2009 15-Oct-2010
15-Nov-2009 15-Nov-2010
15-Dec-2009 15-Dec-2010
15-Jan-2009 15-Jan-2010
15-Feb-2009 15-Feb-2010
15-Mar-2009 15-Mar-2010
15-Apr-2009 15-Apr-2010
15-May-2009 15-May-2010
15-Jun-2009 15-Jun-2010
30-Jun-2009 30-Jun-2010
end data.

* Extract the year from the date  variable, and use it to
* compute the first Monday in July for that year, and the year before.

numeric FirstMonday (date11) / week (f5.0).
compute #year = xdate.year(datevar).
loop #d = 1 to 7.
- if xdate.wkday(date.dmy(#d,7,#year-1)) EQ 2 #FirstMonday1 = date.dmy(#d,7,#year-1).
- if xdate.wkday(date.dmy(#d,7,#year)) EQ 2 #FirstMonday2 = date.dmy(#d,7,#year).
end loop.
compute #week1 = datediff(datevar,#FirstMonday1,'weeks').
compute #week2 = datediff(datevar,#FirstMonday2,'weeks').
do if range(#week1,1,52).
-  compute FirstMonday = #FirstMonday1.
-  compute week = #week1.
else.
-  compute FirstMonday = #FirstMonday2.
-  compute week = #week2.
end if.
list.

--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).
Reply | Threaded
Open this post in threaded view
|

Re: create 1st Monday in July

wsu_wright
In reply to this post by wsu_wright
Bruce,

The script works great (except for a few dates, see below), I like that
it allows me to alter the month/weekday  if the need arises.

However, for the following dates, I get a week value of 0.  Can you see
why that would be the case, is it related to those unique dates or our
we missing something in the syntax.

Thanks again for all your assistance.

David

06-Jul-92
07-Jul-92
08-Jul-92
09-Jul-92
10-Jul-92
11-Jul-92
12-Jul-92
07-Jul-97
08-Jul-97
09-Jul-97
10-Jul-97
11-Jul-97
07-Jul-03
08-Jul-03
09-Jul-03
10-Jul-03
11-Jul-03
12-Jul-03
07-Jul-08
08-Jul-08
09-Jul-08
10-Jul-08
11-Jul-08
12-Jul-08
13-Jul-08



On Thu, Sep 2, 2010 at 7:15 AM, Bruce Weaver wrote:

> David Wright-6 wrote:
>>
>> I have a dataset that daily collects data on student admits that
>> spans
>> several years, for each record there is a date variable (mm/dd/yyyy).
>> I
>> need to be able to extract data & identify the 1st Monday of each
>> week,
>> each year must start on the 1st Monday of July.  So I'm looking for
>> syntax that would allow me to have a week counter (1-52) in which 1
>> always starts on the 1st Monday of July across all the years.  I
>> suspect
>> the xdate function would work, I could extract month & week but I'm
>> not
>> certain how to associated the week num with the 1st week in July
>> since
>> the xdate weeknum would not always be the same for the 1st Monday.
>> Perhaps xdate's wkday would work by capturing the 1st wkday value of
>> 2
>> (monday) for month 7.
>>
>> Thanks in advance,
>>
>> David
>>
>>
>
> How about something like this?
>
> data list free / datevar (date11).
> begin data
> 15-Jul-2009 15-Jul-2010
> 15-Aug-2009 15-Aug-2010
> 15-Sep-2009 15-Sep-2010
> 15-Oct-2009 15-Oct-2010
> 15-Nov-2009 15-Nov-2010
> 15-Dec-2009 15-Dec-2010
> 15-Jan-2009 15-Jan-2010
> 15-Feb-2009 15-Feb-2010
> 15-Mar-2009 15-Mar-2010
> 15-Apr-2009 15-Apr-2010
> 15-May-2009 15-May-2010
> 15-Jun-2009 15-Jun-2010
> 30-Jun-2009 30-Jun-2010
> end data.
>
> * Extract the year from the date  variable, and use it to
> * compute the first Monday in July for that year, and the year before.
>
> numeric FirstMonday (date11) / week (f5.0).
> compute #year = xdate.year(datevar).
> loop #d = 1 to 7.
> - if xdate.wkday(date.dmy(#d,7,#year-1)) EQ 2 #FirstMonday1 =
> date.dmy(#d,7,#year-1).
> - if xdate.wkday(date.dmy(#d,7,#year)) EQ 2 #FirstMonday2 =
> date.dmy(#d,7,#year).
> end loop.
> compute #week1 = datediff(datevar,#FirstMonday1,'weeks').
> compute #week2 = datediff(datevar,#FirstMonday2,'weeks').
> do if range(#week1,1,52).
> -  compute FirstMonday = #FirstMonday1.
> -  compute week = #week1.
> else.
> -  compute FirstMonday = #FirstMonday2.
> -  compute week = #week2.
> end if.
> list.
>
>
>
> -----
> --
> Bruce Weaver
> [hidden email]
> http://sites.google.com/a/lakeheadu.ca/bweaver/
>
> "When all else fails, RTFM."
>
> NOTE: My Hotmail account is not monitored regularly.
> To send me an e-mail, please use the address shown above.
>
> --
> View this message in context:
> http://spssx-discussion.1045642.n5.nabble.com/create-1st-Monday-in-July-tp2800355p2800430.html
> Sent from the SPSSX Discussion mailing list archive at Nabble.com.
>
> =====================
> To manage your subscription to SPSSX-L, send a message to
> [hidden email] (not to SPSSX-L), with no body text except
> the
> command. To leave the list, send the command
> SIGNOFF SPSSX-L
> For a list of commands to manage subscriptions, send the command
> INFO REFCARD

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD