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
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;
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;
Thanks Laurie.. The code works perfectly.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.