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

Hello!

 

Can someone help me to convert a Character datetime type variable into a Date format? thanks

 

example: 

2014-08-24 13:20:00.0000000 -04:00

 

I used this data step to convert character datetime to date, however it's not giving me the result I wanted.

 

data want; set have;
new_date=input(date_entered,anydtdtm.);
format new_date mmddyy10.;
run;

 

 

I'm getting ********* as a result.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Add the datepart() function:

data test;
date_entered = "2014-08-24 13:20:00.0000000 -04:00";
new_date = datepart(input(date_entered,anydtdtm.));
format new_date mmddyy10.;
run;

View solution in original post

9 REPLIES 9
Kurt_Bremser
Super User

Add the datepart() function:

data test;
date_entered = "2014-08-24 13:20:00.0000000 -04:00";
new_date = datepart(input(date_entered,anydtdtm.));
format new_date mmddyy10.;
run;
Eugenio211
Quartz | Level 8

thanks so much, the datepart worked.

Eugenio211
Quartz | Level 8

Hello!

 

I am trying to convert a character date data to numeric to date.  Can someone assist? thanks.

 

Example Data:

2015-04-07 14:35:00.0000000 -04:00

PaigeMiller
Diamond | Level 26

Didn't you ask this question a few days ago? You even marked the answer correct.

 

https://communities.sas.com/t5/SAS-Programming/Character-DateTime-to-Date/m-p/611322#M178165

--
Paige Miller
Tom
Super User Tom
Super User

If you just want the DATE from that sting you can use the INPUT() function with the YYMMDD10. informat to just read the first 10 characters.

Or did you instead want to generate the DATETIME value that the string represents?  If so do you really need to subtract the 4 hours that the tail end seems to indicate?

647   data x;
648    string='2015-04-07 14:35:00.0000000 -04:00';
649    date1=input(string,yymmdd10.);
650    datetime1=input(string,anydtdtm28.);
651    format date1 yymmdd10.  datetime1 datetime20. ;
652    put (_all_) (=/);
653   run;


string=2015-04-07 14:35:00.0000000 -04:00
date1=2015-04-07
datetime1=07APR2015:14:35:00
Eugenio211
Quartz | Level 8

Thank you Tom.

novinosrin
Tourmaline | Level 20

HI @Eugenio211 

Use yymmdd10. informat

 

data want; 

set have; new_date=input(date_entered,yymmdd10.); format new_date mmddyy10.; run;

   

Tom
Super User Tom
Super User

Why did you use the INFORMAT (anydtDTM) that creates a datetime value if you wanted a date value?

Just change the informat to one that generates a date value.

data want; 
  set have;
  new_date=input(date_entered,anydtDTE.);
  format new_date mmddyy10.;
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 14293 views
  • 1 like
  • 5 in conversation