BookmarkSubscribeRSS Feed
SAS_Newbie2024
Calcite | Level 5

I want to make the following make the following dynamic/automated with intnx(), so I don't need to change the macrovariable manually each month I use my code. After I define it, I use it in the where condition of a SQL procedure.

 

%let datetime = '01aug2024 00:00:00'dt;

The following code generates the first day of the last month, however I need it exactly as stated in the static example above.

%let mydt = %sysfunc(intnx(day, %sysfunc(intnx(month, %sysfunc(date()), -1)), 0), Date9.);
%put &mydt;

When I try the following code, which should be a valid solution according to my knowledge, I get 01JAN1960:06:00:00

%let mydt = %sysfunc(intnx(day, %sysfunc(intnx(month, %sysfunc(date()), -1)), 0));
%let mydt = %sysfunc(intnx(hour, &mydt., 0));
%let mydt = %sysfunc(intnx(minute, &mydt., 0));
%let mydt = %sysfunc(intnx(second, &mydt., 0), Datetime.);
%put &mydt;

How can I solve this problem? I tried concatenation and countless other approaches that didn't work and it starts to drive me to despair. Thank you for your advice and help in advance!

3 REPLIES 3
SASJedi
SAS Super FREQ

I think you are overcomplicating the answer. From your example, you always want midnight on the first day of the previous month. If so, just tack on the time as a literal value. To get the resolved text surrounded by single quotes, use the built-in %tslit function. Example:

%let mydt = %tslit(%sysfunc(intnx(day, %sysfunc(intnx(month, %sysfunc(date()), -1)), 0), Date9.) 00:00:00)dt;
%put NOTE: &=mydt;

Result:

NOTE: MYDT='01AUG2024 00:00:00'dt
Check out my Jedi SAS Tricks for SAS Users
Tom
Super User Tom
Super User

If you want to use the value in SAS code then don't bother to format it. Just let %SYSFUNC() generate the number that SAS uses to store that datetime value.

%let datetime = %sysfunc(intnx(dtmonth,%sysfunc(datetime()),-1));

If you want the value to be something a human can understand then you can convert it to a datetime literal by enclosing it in quotes and appending the letters dt.

%let datetime = "%sysfunc(intnx(dtmonth,%sysfunc(datetime()),-1),datetime19.)"dt;

Example:

1    %let datetime1 = %sysfunc(intnx(dtmonth,%sysfunc(datetime()),-1));
2    %let datetime2 = "%sysfunc(intnx(dtmonth,%sysfunc(datetime()),-1),datetime19.)"dt;
3    %put &=datetime1 &=datetime2;
DATETIME1=2038089600 DATETIME2=" 01AUG2024:00:00:00"dt
4    data test;
5      datetime1 = &datetime1;
6      datetime2 = &datetime2;
7      if datetime1=datetime2 then put 'SAME VALUE';
8      else put 'DIFFERENT VALUE';
9    run;

SAME VALUE
NOTE: The data set WORK.TEST has 1 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

Note: The DATETIME format has a bug.  If you attempt to use DATETIME18. you will get only 2 digits for the year.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 308 views
  • 2 likes
  • 4 in conversation