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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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