DATA Step, Macro, Functions and more

To make SAS show human readable datetime if the datetime variable is unformatted

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 19
Accepted Solution

To make SAS show human readable datetime if the datetime variable is unformatted


I have all numerical values like how SAS counts them in days since Jan 1, 1960. I need SAS to show them to me in the format DDMMMYYhh:mm:ss. I tried the following but to no avail

data work.one;

     set work.two;

     format datetimevar DATETIME16.0;

run;

What am I missing?

Thank you.


Accepted Solutions
Solution
‎09-26-2013 01:45 PM
Super User
Super User
Posts: 7,076

Re: To make SAS show human readable datetime if the datetime variable is unformatted

From what I read about STATA it seems to be saying the %tc is for numbers stored in Milliseconds.  Try dividing the numbers you see in your SAS variable by 1000 and then apply a DATETIME. type format.

Your example doesn't look right as when I try that number I get a date in 1997.

26   data _null_;

27    x = '22aug2000:11:00:00'dt;

28    put x= x= datetime19. ;

29    y = 1.1863584E12 ;

30    put y= ;

31    x = y/1000 ;

32    put x= x= datetime19. ;

33   run;

x=1282561200 x=22AUG2000:11:00:00

y=1.1863584E12

x=1186358400 x=05AUG1997:00:00:099

View solution in original post


All Replies
Super User
Posts: 19,865

Re: To make SAS show human readable datetime if the datetime variable is unformatted

datetimes are seconds from jan 1, 1960 not days.

That looks like the default format, so I'm guessing somethings wrong with your dates, eg dates, not datetime or character variables instead of number.

This works as expected:

data one;

var1=dhms(18754, 0, 0, 0);

var2=var1;

format var2 datetime.;

run;

proc print;

run;

Super Contributor
Posts: 307

Re: To make SAS show human readable datetime if the datetime variable is unformatted

As Reeza suggests, your data must be a datetime value to use the DATETIMEw.d format.

If your data is a date value (not datetime), you will have to convert it to a datetime value (e.g. via the DHMS function as Reeza suggests) if you want to apply the DATETIMEw.d format.

If data is a date value, and you just want to display the date in human-readable form and you aren't concerned about hours/minutes/seconds, then use a format like DATE9.

Occasional Contributor
Posts: 19

Re: To make SAS show human readable datetime if the datetime variable is unformatted

Thank you for the suggestions.

My data is imported from a Stata file and has the datetime variable stored as a number (type: double, format: %tc). After importing, in the temporary SAS dataset, this variable shows up as  e.g. 1.1863584E12 which is nothing but 22aug2000 11:00:00. as it shows in the Stata dataset when I open it in the table mode.

Do I still need to convert it to datetime using DHMS and then apply the datetimew.d format?

Thanks a bunch. This is really simple, I know but I have just started.

Solution
‎09-26-2013 01:45 PM
Super User
Super User
Posts: 7,076

Re: To make SAS show human readable datetime if the datetime variable is unformatted

From what I read about STATA it seems to be saying the %tc is for numbers stored in Milliseconds.  Try dividing the numbers you see in your SAS variable by 1000 and then apply a DATETIME. type format.

Your example doesn't look right as when I try that number I get a date in 1997.

26   data _null_;

27    x = '22aug2000:11:00:00'dt;

28    put x= x= datetime19. ;

29    y = 1.1863584E12 ;

30    put y= ;

31    x = y/1000 ;

32    put x= x= datetime19. ;

33   run;

x=1282561200 x=22AUG2000:11:00:00

y=1.1863584E12

x=1186358400 x=05AUG1997:00:00:099

Occasional Contributor
Posts: 19

Re: To make SAS show human readable datetime if the datetime variable is unformatted

Thank you, Tom, Fugue and Reeza.

The last solution totally worked.

🔒 This topic is solved and locked.

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

Discussion stats
  • 5 replies
  • 679 views
  • 9 likes
  • 4 in conversation