Sunday, January 5, 2014

SAS: Force day of week

I had to do some data extracts, but wanted to make sure I got an entire week of data on the front end and back end. Client's fiscal week begins on Thursday. So for a given date, how do I find beginning of fiscal week and ending of fiscal week? Here's what I did as a test program:

* Find fiscal week begin date and week ends date. ;
* Fiscal week always begins on Thursday and ends on Wednesday. ;

data _null_;
format date week_begins week_ends date9.;
date = '04Jan2014'd;
put date=;
week_begins = date - weekday(date) + 5;
week_begins_day = weekday(week_begins);
put week_begins= week_begins_day=;
week_ends = week_begins + 6;
week_ends_day = weekday(week_begins);
put week_ends= week_ends_day=;


week_begins=02JAN2014 week_begins_day=5
week_ends=08JAN2014 week_ends_day=4

(Recall in SAS, weekday function returns 1 for Sunday, 2 for Monday, ... , 7 for Saturday.)