BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
A_SAS_Man
Pyrite | Level 9

I am attempting to set a macro variable to a date two years in the past. I have tried the following code:

 

%let lookback = intnx('year',&sysdate9,-2,same);
%put &lookback;

Which then gives me the following in the log:

 

26         %let lookback = intnx('year',&sysdate9,-2,same);
27         %put &lookback;
intnx('year',25JUL2019,-2,same)

So it doesn't seem to be resolving my variable correctly, but I can't figure out why.

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Just a few minor tweaks.  Try it this way:

 

%let lookback = %sysfunc(intnx(year,%sysevalf("&sysdate9"d),-2,same));
%put &lookback;

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26
%put &sysdate9;
%put %sysfunc(date());

Look at the results of these two commands.

 

INTNX and similar calendar functions requires SAS dates, which are integers, not formatted (and human readable) dates.

 

So &sysdate9 (which produces formatted human readable dates) will not work in INTNX. You need %sysfunc(date()) (which produces a valid SAS date, an integer) in INTNX.

--
Paige Miller
Astounding
PROC Star

Just a few minor tweaks.  Try it this way:

 

%let lookback = %sysfunc(intnx(year,%sysevalf("&sysdate9"d),-2,same));
%put &lookback;
PaigeMiller
Diamond | Level 26

@Astounding wrote:

Just a few minor tweaks.  Try it this way:

 

%let lookback = %sysfunc(intnx(year,%sysevalf("&sysdate9"d),-2,same));
%put &lookback;

Adding to my explanation above, the method from @Astounding works because putting &sysdate9 inside double quotes and then appending the letter d on the end — "&sysdate9"d — turns the human readable date that results from &sysdate9 into an integer which is required by INTNX.

 

My OPINION is that its easier to work with unformatted dates in INTNX and similar functions, rather than format them and then unformat them to get them to work. Naturally, there is a limitation here, if a human has to enter the date into the code (hard code it), then the human has to use some formatted date and you have to convert it to an integer to work in INTNX; but if you want today's date, that doesn't apply.

--
Paige Miller
novinosrin
Tourmaline | Level 20

%let lookback = %sysfunc(intnx(year,"&sysdate9"d,-2,same),date9.);
%put &=lookback;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 2639 views
  • 5 likes
  • 4 in conversation