DATA Step, Macro, Functions and more

Datetime format returning an incorrect date

Reply
Contributor
Posts: 20

Datetime format returning an incorrect date

Hi all

 

data _null_;
%let dateend=%sysfunc(today(),date9.);
%let startdt1=%sysfunc(intnx(month,"&dateend"d,-1,beginning),datetime24.);
run;

 

I am trying to run this and I am getting wierd date for startdt1. I am getting -01JAN1960:05:45:59. I get the same date time stamp however I try to use datetime format. I m unable to figure out why. Why is this happening and what's the fix.

 

Thanks!

Respected Advisor
Posts: 4,173

Re: Datetime format returning an incorrect date

That's because Today() returns a SAS Date (days since 1/1/1960) and not a SAS DateTime value (seconds since 1/1/1960) but format DateTime24. is for SAS DateTime values.

 

If you use format DateTime. for a SAS Date value then you'll end up with 1960 - and 05:45:59 is the result of the number of days treated as number of seconds.

Super User
Posts: 11,343

Re: Datetime format returning an incorrect date

Why the macro calls?

 

Datetimes are measured in seconds. Dates are measured in days. So when you use INTNX on a DATE and use a month interval you get DAYs as a result. The number of days is going to be MUCH smaller than the number of seconds from the base date of 1 Jan 1960. And displaying the number days with a format for seconds means you generally don't get what you want.

To turn a date into a datetime use the function DHMS

 

Datetimevalue=DHMS(datevalue,0,0,0);

will assign a value of midnight at the start of the day.

 

And no real reason to go through the bit of creating a date literal.

 

data _null_;
   dateend=today();
   startdt1 = dhms( intnx('month',today(),-1,'B'),0,0,0);
   put startdt1 datetime24.;
run;

 

 

You would be much better off creating the dates in the datastep with date and time functions and then using call symputx if you need macro variables holding the values. Only bother with date or datetime literals if you as a person are going to read the resolved values. If you are going to use them in further programming such as before, after or between values in data step or where claused best to use the numeric version and simplify code avoiding all the " "d or " "dt issues.

Super User
Posts: 7,761

Re: Datetime format returning an incorrect date

Besides what the others said, your data _null_ is completely useless. The %let macro statements create no code for the data step, so it stays empty.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Ask a Question
Discussion stats
  • 3 replies
  • 454 views
  • 1 like
  • 4 in conversation