How to Convert DATETIME20. TO MM/DD/YYYY HH:MM:SS ?

Reply
Occasional Contributor
Posts: 9

How to Convert DATETIME20. TO MM/DD/YYYY HH:MM:SS ?

Hi ,

I am trying to convert Datetime format of a variable from dataset from DATETIME20.(02MAY2016:21:29:38) to mm/dd/yyyy hh:mm:ss .

Tried to use below PROC FORMAT code but its not working as expected .

 

proc format;

picture mdyhms

other = '%0m/%0d/%Y %0H:%0M:%0S' (datatype=datetime);

run;

 

DATA DATASET2;

    SET DATASET1;

 FORMAT DATEVARIABLE MDYHMS.;

RUN;

 

RESULTS:

INPUT(IN DATASET1) :  02MAY2016:21:29:38

OUTPUT(IN DATSET2) :  1777843778

 

DESIRED OUTPUT(IN DATASET2) : 05/02/2016 21:29:38

 

 

Super User
Posts: 11,116

Re: How to Convert DATETIME20. TO MM/DD/YYYY HH:MM:SS ?

When I run this code:


data junk;
   x=  '02MAY2016:21:29:38'dt;
   put x mdyhms.;
   format x mdyhms.;
run;

The value in the dataset appears as desired and the output in the log. The value you show is the correct value for the datetime variable. Perhaps you have a spelling issue with the name of your variable on the format statement? Do you get a message about an uninitialized variable in the log?

 

Contributor
Posts: 34

Re: How to Convert DATETIME20. TO MM/DD/YYYY HH:MM:SS ?

[ Edited ]

I think you got it right!  When I tried the code, the format was producing the desired output.

 

Internally, sas dates (and datetimes) are always represented as numbers (the format will not change that.)  But when printing/displaying the value, the format should render the output just as you expect it.

 

You may also apply your format to convert the numeric datetime to a string (see example below, using the put function.) Maybe that is what you were looking for.  

 

proc format;
picture mdyhms other = '%0m/%0d/%Y %0H:%0M:%0S' (datatype=datetime);
run;
 
data dataset2;
	format datevariable mdyhms.;
	datevariable = 1777843778;
	datevariable_string = put(datevariable, mdyhms.);
run;

proc print data=dataset2 noobs; 
run;

/* OUTPUT:  */
/* datevariable         datevariable_string  */
/* 05/02/2016 21:29:38  05/02/2016 21:29:38  */ 

 

Ask a Question
Discussion stats
  • 2 replies
  • 1320 views
  • 0 likes
  • 3 in conversation