BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
saurabhc
Fluorite | Level 6


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.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

5 REPLIES 5
Reeza
Super User

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;

Fugue
Quartz | Level 8

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.

saurabhc
Fluorite | Level 6

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.

Tom
Super User Tom
Super User

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

saurabhc
Fluorite | Level 6

Thank you, Tom, Fugue and Reeza.

The last solution totally worked.

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
  • 5 replies
  • 2020 views
  • 9 likes
  • 4 in conversation