DATA Step, Macro, Functions and more

Data format change in SAS - Character to Datetime

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Data format change in SAS - Character to Datetime

Hi there

 

I have a column of data like 201701011200, the format of which is character (YYYYMMDDHHMM). I'd like to transform them to datetime format and I used input() function while it didn't work. Could you guys help me solve this question?

 

Thank you 


Accepted Solutions
Solution
‎06-08-2017 10:01 AM
Super User
Super User
Posts: 7,942

Re: Data format change in SAS - Character to Datetime

Am sure there is an informat for it, but just can't think, so to do it by function:

data want;
  a="201701011200";
  b=dhms(input(substr(a,1,8),yymmdd8.),input(substr(a,9,2),best.),input(substr(a,11,2),best.),0);
  format b datetime.;
run;

View solution in original post


All Replies
Super User
Posts: 19,767

Re: Data format change in SAS - Character to Datetime

INPUT is correct. Post your code and explain what doesn't work means because we can't see your computer Smiley Happy 

If you can't find the correct informat, try ANYDTDTM. 

 


sfps01 wrote:

Hi there

 

I have a column of data like 201701011200, the format of which is character (YYYYMMDDHHMM). I'd like to transform them to datetime format and I used input() function while it didn't work. Could you guys help me solve this question?

 

Thank you 


 

New Contributor
Posts: 3

Re: Data format change in SAS - Character to Datetime

Thank you Reeza for your response. I tried input(COLUMN_NAME, datetime20.) as Datetime_trans and input(COLUMN_NAME, ANYDTDTM.) as Datetime_trans. Neither worked since the column Datetime_trans was blank after I ran my program.

Solution
‎06-08-2017 10:01 AM
Super User
Super User
Posts: 7,942

Re: Data format change in SAS - Character to Datetime

Am sure there is an informat for it, but just can't think, so to do it by function:

data want;
  a="201701011200";
  b=dhms(input(substr(a,1,8),yymmdd8.),input(substr(a,9,2),best.),input(substr(a,11,2),best.),0);
  format b datetime.;
run;
New Contributor
Posts: 3

Re: Data format change in SAS - Character to Datetime

Thank you so much! Your codes works. Appreciate your help

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 116 views
  • 0 likes
  • 3 in conversation