Solved
Contributor
Posts: 38

Nees to create Indicator to show the Hours

Hi ,

I have a dataset that needs to identify the office hours that has opened after 5 PM.

If any of the office hour days that has opened after 5pm needs to create an Indicator Y ; if not indicator has to display as N.

data test;

input Monday \$1-15 Tuesday \$16-31 wednesday \$32-47 Thursday \$48-64 Friday \$65-79 Saturday \$80-96 Sunday \$97-111;

datalines;

08:00AM-05:00PM 08:00AM-02:00PM 08:00AM-02:00PM 08:00AM-03:00PM 08:00AM-03:00PM 08:00AM-06:00PM 08:00AM-02:00PM

08:00AM-05:00PM 08:00AM-05:00PM 08:00AM-05:00PM 08:00AM-05:00PM 08:00AM-05:00PM 08:00AM-05:00PM 08:00AM-05:00PM

08:30AM-02:30PM 08:00AM-02:00PM 08:00AM-02:00PM 08:00AM-02:00PM 08:00AM-02:00PM 08:00AM-02:00PM 08:00AM-06:00PM

10:00AM-02:30PM 08:00AM-02:00PM 08:00AM-06:00PM 08:00AM-02:00PM 08:00AM-02:00PM 08:00AM-02:00PM 08:00AM-02:00PM

09:00AM-02:00PM 08:00AM-02:00PM 08:00AM-02:00PM 08:00AM-02:00PM 08:00AM-02:00PM 08:00AM-02:00PM 08:00AM-02:00PM

04:00AM-09:00PM 08:00AM-05:00PM 08:00AM-05:00PM 08:00AM-05:00PM 08:00AM-05:00PM 08:00AM-05:00PM 08:00AM-06:00PM

;

run;

My code:

data after_5pmHours;

set test;

Monday1 = input(substr(Monday,9,2), 8.);

if Monday1 = 12 and substr(Monday,14,2) = 'PM' then Indicator = 'N';

else if Monday1 > 5 and substr(Monday,14,2) = 'PM' then Indicator = 'Y';

else Indicator = 'N';

run;

Thanks for your help

Accepted Solutions
Solution
‎03-06-2017 09:52 PM
Super Contributor
Posts: 260

Re: Nees to create Indicator to show the Hours

I've simplified the data lines coding, and put it all in one step. Turning the data into SAS time values makes things much easier.

``````data after_5pm;
array days[7] \$ 15 monday tuesday wednesday thursday friday saturday sunday;
input (monday tuesday wednesday thursday friday saturday sunday) (\$15. +1);
indicator = 'N';
do i = 1 to 7 until(indicator = 'Y');
if input(scan(days[i], 2, '-'), time.) > '17:00:00't then
indicator = 'Y';
end;
keep monday tuesday wednesday thursday friday saturday sunday indicator;
datalines;
08:00AM-05:00PM 08:00AM-02:00PM 08:00AM-02:00PM 08:00AM-03:00PM 08:00AM-03:00PM 08:00AM-06:00PM 08:00AM-02:00PM
08:00AM-05:00PM 08:00AM-05:00PM 08:00AM-05:00PM 08:00AM-05:00PM 08:00AM-05:00PM 08:00AM-05:00PM 08:00AM-05:00PM
08:30AM-02:30PM 08:00AM-02:00PM 08:00AM-02:00PM 08:00AM-02:00PM 08:00AM-02:00PM 08:00AM-02:00PM 08:00AM-06:00PM
10:00AM-02:30PM 08:00AM-02:00PM 08:00AM-06:00PM 08:00AM-02:00PM 08:00AM-02:00PM 08:00AM-02:00PM 08:00AM-02:00PM
09:00AM-02:00PM 08:00AM-02:00PM 08:00AM-02:00PM 08:00AM-02:00PM 08:00AM-02:00PM 08:00AM-02:00PM 08:00AM-02:00PM
04:00AM-09:00PM 08:00AM-05:00PM 08:00AM-05:00PM 08:00AM-05:00PM 08:00AM-05:00PM 08:00AM-05:00PM 08:00AM-06:00PM
;
run;``````

All Replies
Solution
‎03-06-2017 09:52 PM
Super Contributor
Posts: 260

Re: Nees to create Indicator to show the Hours

I've simplified the data lines coding, and put it all in one step. Turning the data into SAS time values makes things much easier.

``````data after_5pm;
array days[7] \$ 15 monday tuesday wednesday thursday friday saturday sunday;
input (monday tuesday wednesday thursday friday saturday sunday) (\$15. +1);
indicator = 'N';
do i = 1 to 7 until(indicator = 'Y');
if input(scan(days[i], 2, '-'), time.) > '17:00:00't then
indicator = 'Y';
end;
keep monday tuesday wednesday thursday friday saturday sunday indicator;
datalines;
08:00AM-05:00PM 08:00AM-02:00PM 08:00AM-02:00PM 08:00AM-03:00PM 08:00AM-03:00PM 08:00AM-06:00PM 08:00AM-02:00PM
08:00AM-05:00PM 08:00AM-05:00PM 08:00AM-05:00PM 08:00AM-05:00PM 08:00AM-05:00PM 08:00AM-05:00PM 08:00AM-05:00PM
08:30AM-02:30PM 08:00AM-02:00PM 08:00AM-02:00PM 08:00AM-02:00PM 08:00AM-02:00PM 08:00AM-02:00PM 08:00AM-06:00PM
10:00AM-02:30PM 08:00AM-02:00PM 08:00AM-06:00PM 08:00AM-02:00PM 08:00AM-02:00PM 08:00AM-02:00PM 08:00AM-02:00PM
09:00AM-02:00PM 08:00AM-02:00PM 08:00AM-02:00PM 08:00AM-02:00PM 08:00AM-02:00PM 08:00AM-02:00PM 08:00AM-02:00PM
04:00AM-09:00PM 08:00AM-05:00PM 08:00AM-05:00PM 08:00AM-05:00PM 08:00AM-05:00PM 08:00AM-05:00PM 08:00AM-06:00PM
;
run;``````
Contributor
Posts: 38

Re: Nees to create Indicator to show the Hours

Thanks Laurie.. The code works perfectly.

☑ This topic is solved.

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

Discussion stats
• 2 replies
• 163 views
• 2 likes
• 2 in conversation