BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
MSree01
Fluorite | Level 6
Hi, I am new to SAS, I need to add date macro with variable that it captures date 12 months back from current date . Say if I run code today.. macro should have below date where day (20th of Month) should remain same but year and month can change based on 12 months period.
Run date - current date ( any day in April 2024)
Date macro - March 20, 2023

If run date - May 25th 2024 (Any day in May)
Date macro - April 20, 2023

Appreciate any help
Thank you,
1 ACCEPTED SOLUTION

Accepted Solutions
antonbcristina
SAS Employee

@MSree01 , if you run this code tomorrow it will still generate a back date of 20APR2023. The logic there comes from using the beginning of the month ("B") as a reference point which gives us 01APR2023 and adding 19 days to that will always yield 20th of the month. 

View solution in original post

7 REPLIES 7
antonbcristina
SAS Employee

Welcome to the wonderful world of SAS! According to my calculations, 12 months back from Apr 2024 should be Apr 2023 but maybe there's a reason you need to include the previous month as well. Regardless, the calculation would require the INTNX function which allows you to backtrack by a given time interval, say month. Documentation here: https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p10v3sa3i4kfxfn1sovhi5xzxh8n.h... 

 

Here's an example: 

 

data dates;
	run_date='23apr2024'd;
	back12months_date=intnx('month',run_date,-12,'B')+19;

	format run_date back12months_date date9.;
run;

 

The INTNX function uses 4 arguments here:

  1. The time interval you want to increment/decrement by ("month")
  2. The starting point (run_date)
  3. The increment/decrement value (12 months back would be specified as -12)
  4. The alignment (we will align to the beginning of the month using "B")

I've added 19 days to this calculation to get us to the 20th of the month.

 

You could also make this dynamic by having run_date reference today's date:

 

data dates;
	run_date=today();
	back12months_date=intnx('month',run_date,-12,'B')+19;

	format run_date back12months_date date9.;
run;

which will return back12months_date as 20APR2024.

 

 

Quentin
Super User

Agree with @ballardw , clarifying how you plan to use this date will help us help you.  Also, if you are new to SAS, it's typically better to focus on learning the main SAS language (DATA step) before you learn the macro language.  The job of the macro language is to generate SAS code, so you need to start with a thorough understanding of SAS code. 


That said, here is one way this could be approached in the macro language.  It's basically the macro language version of @antonbcristina 's DATA step approach.

 

%let rundate=%sysfunc(today()) ;
%put &=rundate: Human readable rundate is %sysfunc(putn(&rundate,date9)) ;

%let prioryear=%eval(%sysfunc(intnx(month,&rundate,-12,b)) + 19) ;
%put &=PriorYear: Human readable prior year is %sysfunc(putn(&prioryear,date9));

You can see the macro language is often uglier than the DATA step language.  : )

 

BASUG is hosting free webinars Next up: Mark Keintz presenting History Carried Forward, Future Carried Back: Mixing Time Series of Differing Frequencies on May 8. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
MSree01
Fluorite | Level 6

Hi Quentin,

Thank You for response. I find Data step much easier and yes this one is kinda learn and implement as no down time :).

And I can see how ugly  the macro language :). I am going to use back12months_date as comparative date with other date within my sas code to flag something Y/N. This code is dynamic and runs quarterly except this date is hard coded which I am trying to automate as every quarter, we need to touch the code just to change the back12months_date by extending 3 months, we are trying to avoid the manual touch.

Hope I am able to explain.

 

Thank You all for your valuable inputs and quick responses. Long way to go and lot to learn.

 

Regards,

 

MSree01
Fluorite | Level 6

Hi antonbcristina,

 

Thank You so much for such a quick response and in detail where novice like me can understand.

This code seems giving what I am looking for if I run today.

What If I am running tomorrow? The 20th day will become 21st Day (21st-April 2023). My requirement is anytime, the month and year should change but day should be 20th. So , say if I run your code tomorrow or until end of April, it should still gives me 20th April 2023. Vice versa  if I am running same code on any of day of May 2024, it should give me 20th May 2023. (so 12 months but day should remain same). Hope I am able to explain. I am going to use this as macro within my sas code which compare the data and flag y/n. 

is ther a way to hard code the day as 20th and pass that value and rest of your snippet of macro remain same.

 

I am also meanwhile go through the document which you have provided here.

 

Regards,

MSree

antonbcristina
SAS Employee

@MSree01 , if you run this code tomorrow it will still generate a back date of 20APR2023. The logic there comes from using the beginning of the month ("B") as a reference point which gives us 01APR2023 and adding 19 days to that will always yield 20th of the month. 

MSree01
Fluorite | Level 6

Thank you for quick response on this. I tested it out by hard coding various dates within various months and it does provide 20th as day with 12 months back month and year. I am going to use 12monthsback as variable in my code and will try it out tomorrow by embedding in my original code. But for now going to mark this as accepted solution.

Regards,

MSree

ballardw
Super User

You may want to provide some details of how you expect to use this. Many people experienced with other computer program us "macro" differently than what SAS means internally by "macro". In SAS the macro language is intended to generate code or provide text and may have quite a learning curve.

 

Many places where something like you mention would be used would not require any "macro" elements as SAS functions can capture the date a program runs and then create a date offset as @antonbcristina shows. How to use that offset may vary though. Which is the detail(s) you need to provide.

 

Also, are you or a user ever going to provide the "current date" instead of using the computer's date?

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 360 views
  • 9 likes
  • 4 in conversation