12-18-2017 02:46 PM
When I am doing below program, One of the output is showing today's date and other is 1st Jan 1960.
So the output is changing back to 1960 period if I am using datetime. format.
Is there a way to display current date and time using today() function.
Or please help me in understanding why datetime20. is showing 1960 times?
108? data _null;
109? a=today(); put a date9.;run;
110? data _null;
111? a=today(); put a datetime20.;run;
12-19-2017 11:56 AM - edited 12-19-2017 04:44 PM
Use @HB's solution. I'm a dinosaur and some of the datetime functions don't come to mind quickly.
Time and Datetime values use Second as the interval. Dates use Day as the interval. Both datetime and date values use 1 Jan 1960 as the base and the numeric value is an offset.
When display a date value using datetime format then the days are treated as seconds and hence the result you see in your example.
Combining the functions Today() and Time() with dhms you can capture the date and time;
data example; now = dhms(today(),0,0,time()); format now datetime18.; run;
The function DHMS takes a date (the d), hours, minutes and seconds to create a datetime value. Since the Time() function returns seconds then we tell SAS to use zero hours and minutes and combine the results of Today and Time functions.
12-19-2017 12:02 PM - edited 12-19-2017 04:53 PM
Edit: Read @ballardw 's explanation of what is going on. S/He may or may not actually be a dinosaur (Apatasoarus? Triceritops?), but s/he has a better understanding of the mechanics with offsets and how the information is stored than I do.
today() is going to return days. Not time. So printing today() with a date format provides the expected result.
Printing today with a datetime format does not do what I think you want, which is something like 19DEC2017:10:54:51
For that you need datetime().
data mydate; a=today(); b=datetime(); run; proc print; var a b; format a date9.; format b datetime20.; run;
The SAS System 10:33 Tuesday, December 19, 2017 15
Obs a b
1 19DEC2017 19DEC2017:11:01:16