Determining Weekdays

Accepted Solution Solved
Reply
Regular Contributor
Posts: 183
Accepted Solution

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

6404751012/01/2015
7588651020/01/2015
42222H1020/02/2015
3022551025/05/2015
41014S1010/09/2015
87263U1015/10/2015
0452351012/11/2015
63227W2013/11/2015
6951561016/11/2015
1337661030/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

6404751012/01/2015     Monday
7588651020/01/2015     After holiday
42222H1020/02/2015     Friday
3022551025/05/2015     Monday after Holiday
41014S1010/09/2015     After Holiday
87263U1015/10/2015     Thursday
0452351012/11/2015     After Holiday
63227W2013/11/2015     Friday
6951561016/11/2015     Monday
1337661030/11/2015     Monday after Holiday

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

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/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
;
run;

data holidays;
input holidays  ddmmyy12.;
format  holidays  ddmmyy10.;
cards;
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
;
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

View solution in original post


All Replies
Super User
Posts: 5,255

Re: Determining Weekdays

So what is your question?

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

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/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
;
run;

data holidays;
input holidays  ddmmyy12.;
format  holidays  ddmmyy10.;
cards;
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
;
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: 183

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: 17,750

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: 183

Re: Determining Weekdays

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

Monday

Monday

Tuesday

   Thursday

.

.

.

.

Super User
Posts: 17,750

Re: Determining Weekdays

Try using DOWNAME instead.

Super User
Posts: 9,662

Re: Determining Weekdays

Yes.

week_day=put(issue_date,downame.);

Regular Contributor
Posts: 183

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: 17,750

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: 9,662

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: 183

Re: Determining Weekdays

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

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 11 replies
  • 381 views
  • 6 likes
  • 4 in conversation