SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Auto selecting previous year from Month value

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

Auto selecting previous year from Month value

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;


Accepted Solutions
Solution
‎09-07-2017 02:50 AM
Super User
Posts: 23,296

Re: Auto selecting previous year from Month value

Posted in reply to JordanWood

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


All Replies
Super User
Posts: 6,632

Re: Auto selecting previous year from Month value

Posted in reply to JordanWood

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.

New Contributor
Posts: 4

Re: Auto selecting previous year from Month value

Posted in reply to Astounding

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.

Super User
Posts: 23,296

Re: Auto selecting previous year from Month value

Posted in reply to JordanWood

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. 

New Contributor
Posts: 4

Re: Auto selecting previous year from Month value

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?

 

 

 

Solution
‎09-07-2017 02:50 AM
Super User
Posts: 23,296

Re: Auto selecting previous year from Month value

Posted in reply to JordanWood

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.;
New Contributor
Posts: 4

Re: Auto selecting previous year from Month value

That makes complete sense.

Thanks for the help.
Super User
Posts: 6,632

Re: Auto selecting previous year from Month value

Posted in reply to JordanWood

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).

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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