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-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
  • 3 replies
  • 2760 views
  • 1 like
  • 4 in conversation