BookmarkSubscribeRSS Feed
SASdn
Obsidian | Level 7

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!

3 REPLIES 3
Patrick
Opal | Level 21

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.

ballardw
Super User

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.

Kurt_Bremser
Super User

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.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 3115 views
  • 1 like
  • 4 in conversation