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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.