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!
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;
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;
Thank you! Much Appreciated. I'll give it a try and let you know.
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);
Thank you! I will try this tomorrow morning. Take care!
Thank you Reeza and ChrisNZ! It worked. Appreciate it!
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!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: