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

Hi All,

 

I have a base program that I'm converting to a DI job for automation. Currently in the base job we hard code (and therefore have to manually amend the months) the month folders searched for data compilation. The first thing to note is that the folder names are in 'Month Year' format so 'August 17'. I have already worked out statements to produce this format for the current month and previous month.

 

%let sysmonth= %sysfunc(TODAY(),MONNAME);                                                

%let sysyear= %sysfunc(TODAY(),YEAR2.);  

%let lastmonth= %sysfunc(intnx(month,%sysfunc(TODAY()),-1,e),MONNAME);  

%let currentmonth= &sysmonth &sysyear;

 

The problem i currently have is programing a macro to read the current month and where this is January make the &lastmonth equal to December of the previous year.

 

I have tried various statements and versions of If and Where but have had no luck. The most recent version of my macro is below and any help would be appreciated. ( in the example i'm using September as the trigger month for testing purposes.

 

%let sysmonth = %sysfunc(TODAY(),MONNAME);

%Macro endofyear;

%IF &sysmonth = "September",
%THEN
%let sysyear = /*%sysfunc(intnx(year,%sysfunc(TODAY()),-1,e),YEAR2.)*/

%sysfunc(intnx(year,%sysfunc(&sysyear),-1,e),YEAR2.);
%Else;
%let sysyear = %sysfunc(intnx(year,%sysfunc(TODAY()),YEAR2.);

%run;
%mend endofyear;


%put &sysmonth;
%put $sysyear;

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Pretty much but you nest those a bit. 

 

See this output:

*%let date= %sysfunc(TODAY());
%let date = "01Jan2017"d; /*for testing*/
%let fullcurrentdate= %sysfunc(PUTN(&date, MONNAME)) %sysfunc(PUTN(&date, YEAR2.));

%let prev_date = %sysfunc(intnx(month, &date, -1, s));
%let prevcurrentdate= %sysfunc(PUTN(&prev_date, MONNAME)) %sysfunc(PUTN(&prev_date, YEAR2.));



%put &fullcurrentdate.;
%put &prevcurrentdate.;

View solution in original post

7 REPLIES 7
Astounding
PROC Star

If I understand what you are trying to do, you are already applying the right tools in one place or another.  Consider this statement (from your code):

 

%let lastmonth= %sysfunc(intnx(month,%sysfunc(TODAY()),-1,e),MONNAME);  

 

You could just as easily replace MONNAME with YEAR2, or replace -1 with -13.  Just use INTNX with a MONTH interval, and you'll find it relatively easy to get the year.

JordanWood
Fluorite | Level 6

Hi, thanks for the quick response.

 

I do have the replacement suggested alrready in the Macro. 

 

The issue is that i want the macro to identify the current month and if it is January to automatically apply the previous year to &sysyear for the &lastmonth.

 

At the moment the macro i have included just prints the variable $sysyear as text.

 

Thanks again.

Reeza
Super User

Modified algorithm. 

1. Use TODAY() to get a SAS date, don't format it. 

2. Use INTNX to move to previous month and then format it using Month Year. 

 

Then it doesn't matter when the month is. 

 

You have all the parts there so I think this should be straightforward for you. 

JordanWood
Fluorite | Level 6

Thanks Reeza,

 

That seem to make more sense - i tried something similar to begin with but got put off by formats.

 

Just to confirm are you suggesting something like this;

 

%let date= %sysfunc(TODAY());
%let currentdate= %sysfunc(PUTN(&date, MONTH YEAR)); /*dont think that is a real SAS format*/

 

or 

 

%let date= %sysfunc(TODAY());
%let currentdatemonth= %sysfunc(PUTN(&date, MONNAME));

%let currentdateyear= %sysfunc(PUTN(&date, YEAR2.));

%let fullcurrentdate= &currentdatemonth &currentdateyear

 

and then the same but with Intnx for the previous month/year?

 

 

 

Reeza
Super User

Pretty much but you nest those a bit. 

 

See this output:

*%let date= %sysfunc(TODAY());
%let date = "01Jan2017"d; /*for testing*/
%let fullcurrentdate= %sysfunc(PUTN(&date, MONNAME)) %sysfunc(PUTN(&date, YEAR2.));

%let prev_date = %sysfunc(intnx(month, &date, -1, s));
%let prevcurrentdate= %sysfunc(PUTN(&prev_date, MONNAME)) %sysfunc(PUTN(&prev_date, YEAR2.));



%put &fullcurrentdate.;
%put &prevcurrentdate.;
JordanWood
Fluorite | Level 6
That makes complete sense.

Thanks for the help.
Astounding
PROC Star

I think we're just looking at the elephant from different angles.  This sounds like an easy problem to solve.  Just give two examples.  Based on the date of January 15, 2017, what macro variables would you like to create? For the second example, pick either a December or a February date (whichever you think would be clearest), and again show what macro variables you would like to create.  No code is necessary, just the starting point and the ending point(s).

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 1863 views
  • 1 like
  • 3 in conversation