BookmarkSubscribeRSS Feed
Dob4Die
Calcite | Level 5

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

 

 

2 REPLIES 2
ballardw
Super User

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?

 

carlosmirandad
Obsidian | Level 7

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  */ 

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 15117 views
  • 0 likes
  • 3 in conversation