BookmarkSubscribeRSS Feed
DaveStar
Obsidian | Level 7

Hello friends,

 I need to run a monthly report .

 

This report is using tables of last 6 months in order to create control limits.

 

This report code includes Macro with 2 parameters.(let's say that macro name is Dave).

Each month I need to run 6 macros (Then control limits will be calculated from results of these 6 macro runs)

 

For example: In this month (We are in October 2019) I need to run the following macros:

%Dave(1910,'01OCT2018'd) ;

%Dave(1909,'01SEP2018'd) ;

%Dave(1908,'01AUG2018'd) ;

%Dave(1907,'01JUL2018'd) ;

%Dave(1906,'01JUN2018'd) ;

%Dave(1905,'01MAY2018'd) ;

 

Next month (November 2019) I need to run the following macros:

%Dave(1911,'01NOV2018'd) ;

%Dave(1910,'01OCT2018'd) ;

%Dave(1909,'01SEP2018'd) ;

%Dave(1908,'01AUG2018'd) ;

%Dave(1907,'01JUL2018'd) ;

%Dave(1906,'01JUN2018'd) ;

 

As you can see first parameter of macro has structure of YYMM (year of 2 digits and month of 2 digits).

The reason is that each table name includes YYMM name on it.

 

As you can see second parameter of macro has structure of '01MonthNameYear'd (like date9. format).

The reason is that for each customer I need to check if he/she is a new customer or not.

The criteria is to check date 1 year before YYMM

 

My question:

Let's say that the only user defined variable is  %let last=1910;

and then I need to run all automatically.

How can I do it please?

 

 Thanks

Dave

3 REPLIES 3
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

@DaveStar this is one way you could put those dates into global macros.

data _null_;
	xd = today();
	date1=input(put(xd,yymmdd8.),yymmdd8.);
	 date2 = intnx('month',date1,-1,'e');
 	 date3 = intnx('month',date1,-2,'e');
 	 date4 = intnx('month',date1,-3,'e');
 	 date5 = intnx('month',date1,-4,'e');
 	 date6 = intnx('month',date1,-5,'e');
 	 date7 = intnx('month',date1,-6,'e');
	call symput('month1',put(date2,yymmn6.)); 
	call symput('month2',put(date3,yymmn6.)); 
	call symput('month3',put(date4,yymmn6.));
	call symput('month4',put(date5,yymmn6.));
	call symput('month5',put(date6,yymmn6.));
	call symput('month6',put(date7,yymmn6.));

put date1;
put date2;
put date3;
put date4;
put date5;
put date6;
put date7;
format date1 date2 date3 date4 date5 date6 date7 date9. ;
run;
SASKiwi
PROC Star

@DaveStar  - Try this. Just remove the asterisk in front of "%Dave" to get your macro working:

%let run_date = 28Oct2019;

data _null_;
  date = "&run_date"d;
  format date next_date date9.;
  do i = 0 to -5 by -1;
    next_date = intnx('MONTH', date, i, 'B'); 
    YYMM = compress(put(next_date, yymm6.),'M');
    DDMONYYYY = "'"!!put(next_date, date9.)!!"'d"; 
    call execute('*%Dave('!!YYMM!!','!!DDMONYYYY!!');');
    put _all_;
  end;
run;
Kurt_Bremser
Super User

Three things to add to @SASKiwi's suggestion:

It is rarely (if ever) necessary to store date or datetime literals in macro variables or parameters. It is easier and simpler to use the raw values.

And, if your macro contains macro code (%let, %if or %do), prevent premature execution of such statements with %nrstr.

You also seem to look back one year, so I added that.

next_date = intnx('MONTH', date, i, 'B');
prev_date = intnx('year',next_date,-1,'s');
YYMM = compress(put(next_date, yymm6.),'M');
call execute(cats('%nrstr(%Dave(',YYMM,',',prev_date,'));'));

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 624 views
  • 1 like
  • 4 in conversation