DATA Step, Macro, Functions and more

time comparison in SAS

Accepted Solution Solved
Reply
Super Contributor
Posts: 312
Accepted Solution

time comparison in SAS

[ Edited ]

I have wagerdatetime variable, and I want to create a new variable timezone, based on the timepart(wagerdatetime):

if 3-9 am, timeslot ="1", 9am-3pm, timeslot="2", 3-9 pm timeslot="3", 9pm-3am, timeslot="4".

 

The first three are okay, but the 4th one, 9pm-3am the next day, how should I do it?

maybe I should say 0:00:00-3:00:00  & 21:00"00-23:59:59?

if '3:00:00'T <timepart(wagerdatetime) <='9:00:00'T then timeslot="A";
if '9:00:00'T <timepart(wagerdatetime) <='15:00:00'T then timeslot="B";
if '15:00:00'T <timepart(wagerdatetime) <='21:00:00'T then timeslot="C";
if '21:00:00'T <timepart(wagerdatetime) <='3:00:00'T then timeslot="D";
run;

 

I used

if '21:00:00'T <timepart(wagerdatetime) <='23:59:59'T then timeslot="D";
if '00:00:00'T =<timepart(wagerdatetime) <='3:00:00'T then timeslot="D";
run;

but I still have 3 with timeslot missing:

08JUN14:00:00:00

26JUN14:23:59:59

12MAY14:00:00:00

 

all others are fine.

 

 

 


Accepted Solutions
Solution
‎04-26-2016 12:14 PM
Trusted Advisor
Posts: 1,115

Re: time comparison in SAS

[ Edited ]

Or replace '23:59:59'T in the IF condition by '24:00:00'T in order to close the 1-second gap between 23:59:59 and 00:00:00.

View solution in original post


All Replies
Super User
Super User
Posts: 7,401

Re: time comparison in SAS

Hi,

 

if <condition> then;

else if <conition> then;

else;

 

So someting like:

if '3:00:00'T <timepart(wagerdatetime) <='9:00:00'T then timeslot="A";
else if '9:00:00'T <timepart(wagerdatetime) <='15:00:00'T then timeslot="B";
else if '15:00:00'T <timepart(wagerdatetime) <='21:00:00'T then timeslot="C";
else timeslot="D";run;
Super Contributor
Posts: 312

Re: time comparison in SAS

[ Edited ]
if '3:00:00'T <timepart(wagerdatetime) <='9:00:00'T then timeslot="A";
else if '9:00:00'T <timepart(wagerdatetime) <='15:00:00'T then timeslot="B";
else if '15:00:00'T <timepart(wagerdatetime) <='21:00:00'T then timeslot="C";
else if '21:00:00'T <timepart(wagerdatetime) <='23:59:59'T then timeslot="D";
else if '00:00:00'T =<timepart(wagerdatetime) <='3:00:00'T then timeslot="D";
run;

still got the three with timeslot missing

08JUN14:00:00:00

26JUN14:23:59:59

12MAY14:00:00:00

Respected Advisor
Posts: 3,777

Re: time comparison in SAS

[ Edited ]
 
Solution
‎04-26-2016 12:14 PM
Trusted Advisor
Posts: 1,115

Re: time comparison in SAS

[ Edited ]

Or replace '23:59:59'T in the IF condition by '24:00:00'T in order to close the 1-second gap between 23:59:59 and 00:00:00.

Super Contributor
Posts: 312

Re: time comparison in SAS

Yes. this worked for the 3 missing

Thank you.
Super User
Super User
Posts: 7,401

Re: time comparison in SAS

If you look at my code, the final part 4 one is:

else timeslot="D";

I.e. if it doesn't fit into the other three then it is D, you don't need the final conditional, it can only be 1, 2, 3 or else D.

Super Contributor
Posts: 312

Re: time comparison in SAS

[ Edited ]

@RW9  I did not see your code earlier. Sorry. Actually using else is a better/simpler way.

 

I guess the "quick reply" inserted your post on the top but I was looking at bottom, so I missed your good suggestion.

☑ This topic is SOLVED.

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

Discussion stats
  • 7 replies
  • 377 views
  • 1 like
  • 4 in conversation