BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lydiawawa
Lapis Lazuli | Level 10

Hi All,

 

I have a string variable with the following type of value:

04-27-2018 09:41:50:815

 

Code  that I used to format the variable to datetime is as of the following:

data test;
   set test;
   date1 = input(date,anydtdtm30.);
   format date1 datetime30.;
run;

Instead of converting values to date and time, the returned values are all blank.

Does anyone know where is the issue is?

 

Thanks!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
SuryaKiran
Meteorite | Level 14

You can ignore the milliseconds if not required. You can use @Kurt_Bremser approach with some other functions,

 


data test;
str="04-27-2018 09:41:50:815";
DATETIME=DHMS( input(scan(str,1," "),mmddyy10.),     /* Date */
			   input(scan(scan(str,2," "),1,':'),2.),/* Hour */
			   input(scan(scan(str,2," "),2,':'),2.),/* Min  */
			   input(scan(scan(str,2," "),3,':'),2.) /* Sec  */
			  );
format datetime datetime26.;
run;
Thanks,
Suryakiran

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

Instead of letting the any.... informat make guesses, I'd dissect the string into the time and date part, input those separately with the fitting informats, and then build the datetime by doing

datetime = dhms(date,0,0,time);

 

novinosrin
Tourmaline | Level 20

I agree with @Kurt_Bremser, along those lines

 

data w;
date='04-27-2018 09:41:50:815';
date_extract=input(substr(date,1,10),mmddyy10.);
time_extract=input(substr(date,11),time.);
new_date_time=dhms(date_extract,0,0,0)+time_extract;*combine date and time extract;
format new_date_time datetime30.;
run;
ballardw
Super User

@lydiawawa wrote:

Hi All,

 

I have a string variable with the following type of value:

04-27-2018 09:41:50:815

 

Code  that I used to format the variable to datetime is as of the following:

data test;
   set test;
   date1 = input(date,anydtdtm30.);
   format date1 datetime30.;
run;

Instead of converting values to date and time, the returned values are all blank.

Does anyone know where is the issue is?

 

Thanks!

 

 


Not actually surprised. That last colon really should be a decimal to indicate fractional seconds.

If you don't NEED the fractional seconds:

data example;
  str='04-27-2018 09:41:50:815';
  dt = input(substr(str,1,19),anydtdtm.);
  format dt datetime.;
run;
SuryaKiran
Meteorite | Level 14

You can ignore the milliseconds if not required. You can use @Kurt_Bremser approach with some other functions,

 


data test;
str="04-27-2018 09:41:50:815";
DATETIME=DHMS( input(scan(str,1," "),mmddyy10.),     /* Date */
			   input(scan(scan(str,2," "),1,':'),2.),/* Hour */
			   input(scan(scan(str,2," "),2,':'),2.),/* Min  */
			   input(scan(scan(str,2," "),3,':'),2.) /* Sec  */
			  );
format datetime datetime26.;
run;
Thanks,
Suryakiran

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
  • 4 replies
  • 976 views
  • 3 likes
  • 5 in conversation