DATA Step, Macro, Functions and more

SAS datetime format

Reply
Contributor
Posts: 52

SAS datetime format

Hi Members,

 

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;

18DEC2017


110? data _null;
111? a=today(); put a datetime20.;run;

01JAN1960:05:52:51

 

Thanks,

Suchi

Super User
Posts: 13,023

Re: SAS datetime format

[ Edited ]
Posted in reply to suchismita

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.

 

Super Contributor
Super Contributor
Posts: 260

Re: SAS datetime format

[ Edited ]
Posted in reply to suchismita

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;

results are

 

                                         The SAS System     10:33 Tuesday, December 19, 2017  15

                            Obs            a                       b

                             1     19DEC2017      19DEC2017:11:01:16

Ask a Question
Discussion stats
  • 2 replies
  • 67 views
  • 3 likes
  • 3 in conversation