Dynamically Pull 3 Months of Data in a SAS Code With Macro

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

Dynamically Pull 3 Months of Data in a SAS Code With Macro

Hello Fellow SAS Experts,

 

I'm a newbie in SAS. I would need help from the SAS community to optimize a SAS code I'm working on. 

 

The current code is as follows:

--------------------------------------------------------------------------------------------------------------------------------------------

%macro sales (outdata, date)

[

Create Table

   Select data

%mend sales

]

/* The current code invokes macro manually as */

%Sales (Outdata = Sales_Aug, date = '2017-08-01');

%Sales (Outdata = Sales_Sept, date = '2017-09-01');

%Sales (Outdata = Sales_Oct, date = '2017-10-01');

 

/* So every month I've to manually enter last 3 months as input to the macro */

 

Is there a way, I can make the macro call dynamic? So that If I run the code in November, it selects data from last 3 months (Aug, Sept, Oct) and If I run the code in December, it gives data from Sept, Oct, Nov?

 

I apologize, if it's a very basic question. Any help would be much appreciated. Take care!

 


Accepted Solutions
Solution
‎11-07-2017 11:30 AM
Super User
Posts: 21,546

Re: Dynamically Pull 3 Months of Data in a SAS Code With Macro

Posted in reply to SteelersPitts

1. Use INTNX to increment dates

2. Use today() to get the current date

3. Use CALL EXECUTE to call the macro multiple times from your data _null_ step. See the examples in the documentation.

 

This isn’t working code, you’ll need to debug/test it. You can also make it more efficient by making it loop, but for three runs that’s not too important. You can get the Month Name by using the format MonName3.

 

Data _null_;
Date = intnx(‘month’, today(), 0, ‘b’);
Date_Last_Month = intnx(‘month’, date, -1, ‘b’);
Date_Month2 = intnx(‘month’, date, -2, ‘b’);

Month_text = put(date, monname3.);
Str1 = catt(‘%sales(outdata=Sales’, month_text, ‘,date=‘, put(date, yymmddd10.), ‘);’); Call execute(str1); Str2 = ... Str3 = .... Run;

View solution in original post


All Replies
Solution
‎11-07-2017 11:30 AM
Super User
Posts: 21,546

Re: Dynamically Pull 3 Months of Data in a SAS Code With Macro

Posted in reply to SteelersPitts

1. Use INTNX to increment dates

2. Use today() to get the current date

3. Use CALL EXECUTE to call the macro multiple times from your data _null_ step. See the examples in the documentation.

 

This isn’t working code, you’ll need to debug/test it. You can also make it more efficient by making it loop, but for three runs that’s not too important. You can get the Month Name by using the format MonName3.

 

Data _null_;
Date = intnx(‘month’, today(), 0, ‘b’);
Date_Last_Month = intnx(‘month’, date, -1, ‘b’);
Date_Month2 = intnx(‘month’, date, -2, ‘b’);

Month_text = put(date, monname3.);
Str1 = catt(‘%sales(outdata=Sales’, month_text, ‘,date=‘, put(date, yymmddd10.), ‘);’); Call execute(str1); Str2 = ... Str3 = .... Run;
Occasional Contributor
Posts: 9

Re: Dynamically Pull 3 Months of Data in a SAS Code With Macro

Thank you! Much Appreciated. I'll give it a try and let you know. 

PROC Star
Posts: 1,935

Re: Dynamically Pull 3 Months of Data in a SAS Code With Macro

Posted in reply to SteelersPitts

Like this?


%macro sales(date);
  %local monthno sasdate outdata date;
  %do monthno = 0 %to 2;
    %let sasdate = %sysfunc(intnx(month,%sysfunc(inputn(&date,yymmdd10.)),&monthno));
    %let outdata = SALES_%sysfunc(putn(&sasdate,monname3.));   %* bad name, do not use such a suffix;
    %put &=outdata;
    %let outdata = SALES_%sysfunc(putn(&sasdate,yymmn6.));     %* use this table suffix instead;
    %let dateq   = %unquote(%nrbquote('%sysfunc(putn(&sasdate,yymmddd10.))'));
    data &outdata.;
      X=&dateq.; 
    run;
  %end;
%mend;
%sales(2017-10-01);

 

 

 

Occasional Contributor
Posts: 9

Re: Dynamically Pull 3 Months of Data in a SAS Code With Macro

Thank you! I will try this tomorrow morning. Take care!

Occasional Contributor
Posts: 9

Re: Dynamically Pull 3 Months of Data in a SAS Code With Macro

Posted in reply to SteelersPitts

Thank you Reeza and ChrisNZ! It worked. Appreciate it!

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 157 views
  • 2 likes
  • 3 in conversation