BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
cho16
Obsidian | Level 7

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

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
LaurieF
Barite | Level 11

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;

View solution in original post

2 REPLIES 2
LaurieF
Barite | Level 11

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;
cho16
Obsidian | Level 7

Thanks Laurie.. The code works perfectly.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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