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

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 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

5 REPLIES 5
Reeza
Super User

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;
SteelersPitts
Obsidian | Level 7

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

ChrisNZ
Tourmaline | Level 20

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

 

 

 

SteelersPitts
Obsidian | Level 7

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

SteelersPitts
Obsidian | Level 7

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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