05-24-2016 10:38 AM
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 .
other = '%0m/%0d/%Y %0H:%0M:%0S' (datatype=datetime);
FORMAT DATEVARIABLE MDYHMS.;
INPUT(IN DATASET1) : 02MAY2016:21:29:38
OUTPUT(IN DATSET2) : 1777843778
DESIRED OUTPUT(IN DATASET2) : 05/02/2016 21:29:38
05-24-2016 11:07 AM
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?
05-24-2016 11:09 AM - edited 05-24-2016 11:16 AM
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 */