DATA Step, Macro, Functions and more

SAS Date and Time

Accepted Solution Solved
Reply
SAS Employee
Posts: 73
Accepted Solution

SAS Date and Time

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


Accepted Solutions
Solution
‎06-27-2011 05:58 PM
SAS Super FREQ
Posts: 8,868

Re: SAS Date and Time

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


All Replies
Solution
‎06-27-2011 05:58 PM
SAS Super FREQ
Posts: 8,868

Re: SAS Date and Time

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.

Contributor
Posts: 23

SAS Date and Time

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

SAS Employee
Posts: 73

SAS Date and Time

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

20110101

Thanks

Frequent Contributor
Posts: 81

Re: SAS Date and Time

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

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 1292 views
  • 0 likes
  • 4 in conversation