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
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?
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 */
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.