## Determining Weekdays

Solved
Regular Contributor
Posts: 194

# Determining Weekdays

Hi experts, please take a look at the sample my two datasets. I need to determine the week days for the issue_date in the first dataset, for example, monday, tuesday etc. However, if the issue_date is after any holiday (holidays are in the second dataset) then that will not be week days, rather that will be after holiday (instead of monday, tuesday etc.). But, if the issue_date was Monday and if there was no holiday on prior Friday then it will be Monday, but if the holiday was prior Friday, then it will not be Monday, it will be Monday after holiday.

The first dataset looks as follows:

cusip          issue_date

 64047510 12/01/2015 75886510 20/01/2015 42222H10 20/02/2015 30225510 25/05/2015 41014S10 10/09/2015 87263U10 15/10/2015 04523510 12/11/2015 63227W20 13/11/2015 69515610 16/11/2015 13376610 30/11/2015

The second dataset looks as follows:

holidays

01/01/2015

19/01/2015

16/02/2015

22/05/2015

09/09/2015

12/10/2015

11/11/2015

27/11/2015

25/12/2015

So the output will look like:

cusip          issue_date     weekday

 64047510 12/01/2015     Monday 75886510 20/01/2015     After holiday 42222H10 20/02/2015     Friday 30225510 25/05/2015     Monday after Holiday 41014S10 10/09/2015     After Holiday 87263U10 15/10/2015     Thursday 04523510 12/11/2015     After Holiday 63227W20 13/11/2015     Friday 69515610 16/11/2015     Monday 13376610 30/11/2015     Monday after Holiday

Accepted Solutions
Solution
‎08-29-2015 11:48 PM
Super User
Posts: 10,787

## Re: Determining Weekdays

Your question is really hard to understand.

Suppose I know what you are looking for.

### Code: Program

`data have;infile cards expandtabs;input cusip  : \$20.   issue_date : ddmmyy12.;format issue_date ddmmyy10.;cards;64047510 12/01/201575886510 20/01/201542222H10 20/02/201530225510 25/05/201541014S10 10/09/201587263U10 15/10/201504523510 12/11/201563227W20 13/11/201569515610 16/11/201513376610 30/11/2015;run; data holidays;input holidays  ddmmyy12.;format  holidays  ddmmyy10.;cards;01/01/201519/01/201516/02/201522/05/201509/09/201512/10/201511/11/201527/11/201525/12/2015;run;data want;if _n_ eq 1 then do; if 0 then set holidays; declare hash ha(dataset:'holidays'); ha.definekey('holidays'); ha.definedone();end; set have; week_day=scan(put(issue_date,weekdate.),1,','); if ha.check(key:(issue_date-1))=0 then week_day='After holiday'; if weekday(issue_date)=2 and ha.check(key:(issue_date-3))=0 then week_day='Monday after Holiday';run;`

Xia Keshan

All Replies
Super User
Posts: 5,884

## Re: Determining Weekdays

Data never sleeps
Solution
‎08-29-2015 11:48 PM
Super User
Posts: 10,787

## Re: Determining Weekdays

Your question is really hard to understand.

Suppose I know what you are looking for.

### Code: Program

`data have;infile cards expandtabs;input cusip  : \$20.   issue_date : ddmmyy12.;format issue_date ddmmyy10.;cards;64047510 12/01/201575886510 20/01/201542222H10 20/02/201530225510 25/05/201541014S10 10/09/201587263U10 15/10/201504523510 12/11/201563227W20 13/11/201569515610 16/11/201513376610 30/11/2015;run; data holidays;input holidays  ddmmyy12.;format  holidays  ddmmyy10.;cards;01/01/201519/01/201516/02/201522/05/201509/09/201512/10/201511/11/201527/11/201525/12/2015;run;data want;if _n_ eq 1 then do; if 0 then set holidays; declare hash ha(dataset:'holidays'); ha.definekey('holidays'); ha.definedone();end; set have; week_day=scan(put(issue_date,weekdate.),1,','); if ha.check(key:(issue_date-1))=0 then week_day='After holiday'; if weekday(issue_date)=2 and ha.check(key:(issue_date-3))=0 then week_day='Monday after Holiday';run;`

Xia Keshan

Regular Contributor
Posts: 194

## Re: Determining Weekdays

Thanks Boss. My question is what I want to determine. I said that I need to determine . ............ . I am sorry for not writing the question clearly. But you rightly understand my question.

Your code works fine. But why did you  ',' in the following line?

week_day=scan(put(issue_date,weekdate.),1,',');

Super User
Posts: 23,774

## Re: Determining Weekdays

RTM - SCAN function - third parameter indicates the delimiter, in this case a comma.

The put function converts the date to the WEEKDATE. Format - you can find the specifications here:

scan function

SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition

weekdate. format

SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition

He could have used the DOWNAME format instead though and avoided the scan function

SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition

Regular Contributor
Posts: 194

## Re: Determining Weekdays

But if I use the ',' then week_day has observations like this:

Monday

Monday

Tuesday

Thursday

.

.

.

.

Super User
Posts: 23,774

Super User
Posts: 10,787

## Re: Determining Weekdays

Yes.

week_day=put(issue_date,downame.);

Regular Contributor
Posts: 194

## Re: Determining Weekdays

Ok.

But if I remove the ',' from that line, then it shows rightly i.e. the day names without space like

Monday

Tuesday

.

.

..

Is it ok to just remove ',' ?

Super User
Posts: 23,774

## Re: Determining Weekdays

RTM - If you remove the third parameter from the scan function, what is the default delimiter to the scan function? Given what the date would look like (see below) would that make sense?

Super User
Posts: 10,787

## Re: Determining Weekdays

That should be OK. SCAN will default take comma or blank as a delimiter . Therefore, they might be the same, just check it carefully, make sure you got right result.

Regular Contributor
Posts: 194

## Re: Determining Weekdays

Thanks. I think results are right. There is no space before the week days if I remove ','

🔒 This topic is solved and locked.