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

I have a variable with dates and time values such as:

22SEP15:15:10:35
06JUN16:15:03:53

I would like to create three new variables based on if the value start with 01-10,11-20 or 21-31.
But I am having problems only picking up the information of the first two digits.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Sounds like you want to get to the day of the month.

day(datepart(datetime_var))

And generate a new variable with 3 values.

day=day(datepart(datetime_var));
if 0<day<=10 then month_part=1;
else if day <=20 then month_part=2;
else if day <=31 then month_part=3;
else month_part=.;

View solution in original post

5 REPLIES 5
andreas_lds
Jade | Level 19

Do you have a numeric variable formatted as datetime (aka proper sas datetime) or a string containing a datetime-value? When working with date and time data it is almost always useful to convert the data to a sas datetime.

 

And unfortunately i can't see any connection between the datetimes and the new variables.

Fredron
Fluorite | Level 6

It seems like I have SAS datetime.

For the regression I am running I would like to see if it matters if it's the beginning, middle or the end of a month.
So I would like all events that happened during the 01-10 date of the months in one category, the events that happened during day 11-20 of the months in another and the last 21-31 in a third category.
But I am stuck, I just want SAS to check the first two numbers and sort after that, but can't get it to work.

Thanks for all help

Tom
Super User Tom
Super User

Sounds like you want to get to the day of the month.

day(datepart(datetime_var))

And generate a new variable with 3 values.

day=day(datepart(datetime_var));
if 0<day<=10 then month_part=1;
else if day <=20 then month_part=2;
else if day <=31 then month_part=3;
else month_part=.;
PeterClemmensen
Tourmaline | Level 20

Hi and welcome to the SAS Community 🙂


Are these actual datetime values (numeric) or character values?

Fredron
Fluorite | Level 6

I'm not sure to be honest, I just imported a dataset and did proc print to have a look at it.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 1037 views
  • 1 like
  • 4 in conversation