BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
JohanK
Calcite | Level 5

Dear fellow SAS users, firstly I hope to achieve the level to be able to help others but I am afraid that may take a while 🙂 I have a dataset with dates (Date_and_time_of_assessment) in Character format like this: 2023-01-26T07:00. 

I want to convert these values into a numeric format so I can use them for calculation purposes. 

I have tried SAMP_DT=INPUT(Date_and_time_of_assessment, anydtdtm16.);  but that does not work. Could anyone help me, it is much appreciated. Thanks! Johan

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

What specific calculation? No example given.

 

You should learn to look at the SAS online help for date, time and datetime related formats and informats. There is one for that specific appearance shown below and a number of example calculations using the result.

data example;
   string="2023-01-26T07:00";
   dt = input(string,E8601dt.);
   put dt= datetime20.;
   /* calculations: add 3 months*/
   newdt= intnx('dtmonth',dt,3,'s');
   put newdt= datetime20.;
   /* go back 5 years*/
   newdt= intnx('dtyear',dt,-5,'s');
   put newdt= datetime20.;
   /* difference in days from today*/
   dif = intck('dtday', datetime(),dt,'c');
   put dif= ;
run;

View solution in original post

6 REPLIES 6
Kurt_Bremser
Super User

This is the ISO 8601 extendeed format, for which SAS provides a dedicated informat:

SAMP_DT = input(Date_and_time_of_assessment,e8601dt16.);

To extract the date from this datetime value, use the DATEPART function. Don't forget to attach a suitable display format to the new variable.

 

ASTEROID-SIZED HINT: never just say "it does not work". If there are ERRORs, WARNINGs or curious NOTEs, post the complete log of the step. If the step runs without obvious messages in the log, also post the result and describe in detail where it does not meet your expectations.

"Does not work" on its own is worthy of the proverbial person who can't find their way out of a phone booth without expert help.

JohanK
Calcite | Level 5

Thanks so much Kurt!  I got help from more users with the solution. And apologize for my wording, I meant to say that I was not able to find the solution my self instead of 'it did not work'. Appreciated 

greetings, Johan 

ballardw
Super User

What specific calculation? No example given.

 

You should learn to look at the SAS online help for date, time and datetime related formats and informats. There is one for that specific appearance shown below and a number of example calculations using the result.

data example;
   string="2023-01-26T07:00";
   dt = input(string,E8601dt.);
   put dt= datetime20.;
   /* calculations: add 3 months*/
   newdt= intnx('dtmonth',dt,3,'s');
   put newdt= datetime20.;
   /* go back 5 years*/
   newdt= intnx('dtyear',dt,-5,'s');
   put newdt= datetime20.;
   /* difference in days from today*/
   dif = intck('dtday', datetime(),dt,'c');
   put dif= ;
run;
JohanK
Calcite | Level 5

Thank you so much! Your solution works perfect. And sorry for my expression that 'it did not work'. I meant actually that I was not able to find the solution - there are dozens of pages online with date informats but I did not find the correct one.  With your solution I am able to substract 2 dates from eachother . Great! 

PaigeMiller
Diamond | Level 26

@JohanK wrote:

Dear fellow SAS users, firstly I hope to achieve the level to be able to help others but I am afraid that may take a while 🙂 I have a dataset with dates (Date_and_time_of_assessment) in Character format like this: 2023-01-26T07:00. 

I want to convert these values into a numeric format so I can use them for calculation purposes. 

I have tried SAMP_DT=INPUT(Date_and_time_of_assessment, anydtdtm16.);  but that does not work. Could anyone help me, it is much appreciated. Thanks! Johan


I'm skeptical that this is a character "format", as there really isn't such a thing. It is either a numeric variable, or a character (or text) variable. In order to know how to handle this, we need to know from PROC CONTENTS if the variable is numeric or character. So please show us the PROC CONTENTS output for this variable.

 

Agreeing with the others, saying "that doesn't work", without additional information, has no value and doesn't lead us closer to an answer.

--
Paige Miller
JohanK
Calcite | Level 5

Thanks for your reaction- I have a solution, this platform is great. Appreciated.... 

 

Kind regards, Johan

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!
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
  • 6 replies
  • 471 views
  • 0 likes
  • 4 in conversation