I have a datetime in character format 2011-01-01-08.35.26.
How do i convert this into two seprate columns one for date and one for time ?
Thanks
Hi:
Basically, you need the INPUT function to convert your character string into a DATE/TIME variable that represents the number of seconds since midnight Jan 1, 1960. Then, once you have that number, you want to extract the date only using the DATEPART function and extract the time only using the TIMEPART function. The INPUT function needs to know how to convert your value from a character to numeric variable, so it can use the ANYDTDTM informat in order to do the conversion. A model program and the output (in the SAS Log) is shown below.
cynthia
97 data convert;
98 charvar = '2011-01-01-08.35.26';
99 numdt = input(charvar,anydtdtm.);
100 date = datepart(numdt);
101 time = timepart(numdt);
102 put charvar= numdt= date= mmddyy10. time= time8.;
103 run;
charvar=2011-01-01-08.35.26 numdt=1609490126 date=01/01/2011 time=8:35:26
NOTE: The data set WORK.CONVERT has 1 observations and 4 variables.
Hi:
Basically, you need the INPUT function to convert your character string into a DATE/TIME variable that represents the number of seconds since midnight Jan 1, 1960. Then, once you have that number, you want to extract the date only using the DATEPART function and extract the time only using the TIMEPART function. The INPUT function needs to know how to convert your value from a character to numeric variable, so it can use the ANYDTDTM informat in order to do the conversion. A model program and the output (in the SAS Log) is shown below.
cynthia
97 data convert;
98 charvar = '2011-01-01-08.35.26';
99 numdt = input(charvar,anydtdtm.);
100 date = datepart(numdt);
101 time = timepart(numdt);
102 put charvar= numdt= date= mmddyy10. time= time8.;
103 run;
charvar=2011-01-01-08.35.26 numdt=1609490126 date=01/01/2011 time=8:35:26
NOTE: The data set WORK.CONVERT has 1 observations and 4 variables.
Hi,
Great code from Cynthia, the only thing I can add is if you still want the date formatted with dashes yyyy-mm-dd you can use yymmddd10.. The third d scpecifies the dashes.
Rich
I actully need the date to be without dashes. What format do i need to use to get rid of the dashes ?
20110101
Thanks
data convert;
charvar = '2011-01-01-08.35.26';
numdt = input(charvar,anydtdtm.);
date = datepart(numdt);
time = timepart(numdt);
put charvar= numdt= date= yymmddn8. time= time8.;
run;
charvar=2011-01-01-08.35.26 numdt=1609490126 date=20110101 time=8:35:26
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.
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.