BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
newbi
SAS Employee

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

1 ACCEPTED SOLUTION

Accepted Solutions
Cynthia_sas
SAS Super FREQ

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.

View solution in original post

4 REPLIES 4
Cynthia_sas
SAS Super FREQ

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.

rtritz
Calcite | Level 5

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

newbi
SAS Employee

I actully need the date to be without dashes.  What format do i need to use to get rid of the dashes ?

20110101

Thanks

NickR
Quartz | Level 8

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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 2789 views
  • 0 likes
  • 4 in conversation