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

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.

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

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

7 REPLIES 7
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
fengyuwuzu
Pyrite | Level 9
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

data_null__
Jade | Level 19
 
FreelanceReinh
Jade | Level 19

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.

fengyuwuzu
Pyrite | Level 9
Yes. this worked for the 3 missing

Thank you.
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

fengyuwuzu
Pyrite | Level 9

@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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 7 replies
  • 6649 views
  • 1 like
  • 4 in conversation