SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

Hello

I need to Run series of macros for each date from 23AUG2020 until 05SEP2020.

What is the way to perform it with minimum code length?

One more issue that the the dates of running will not include Saturday and Sunday because it is not  business days

 


%MMacro(date='23AUG2020'd,CAT=X1,Ffmt=FF1F.);
%MMacro(date='23AUG2020'd,CAT=X2,Ffmt=FF2F.);
%MMacro(date='23AUG2020'd,CAT=X3,Ffmt=FF3F.);
%MMacro(date='23AUG2020'd,CAT=X4,Ffmt=FF4F.);
%MMacro(date='23AUG2020'd,CAT=X5,Ffmt=FF5F.);
%MMacro(date='23AUG2020'd,CAT=X6,Ffmt=FF6F.);
%MMacro(date='23AUG2020'd,CAT=X7,Ffmt=FF7F.);
%MMacro(date='23AUG2020'd,CAT=X8,Ffmt=FF8F.);
%MMacro(date='23AUG2020'd,CAT=X9,Ffmt=FF9F.);
%MMacro(date='23AUG2020'd,CAT=X10,Ffmt=FF10F.);


%MMacro(date='24AUG2020'd,CAT=X1,Ffmt=FF1F.);
%MMacro(date='24AUG2020'd,CAT=X2,Ffmt=FF2F.);
%MMacro(date='24AUG2020'd,CAT=X3,Ffmt=FF3F.);
%MMacro(date='24AUG2020'd,CAT=X4,Ffmt=FF4F.);
%MMacro(date='24AUG2020'd,CAT=X5,Ffmt=FF5F.);
%MMacro(date='24AUG2020'd,CAT=X6,Ffmt=FF6F.);
%MMacro(date='24AUG2020'd,CAT=X7,Ffmt=FF7F.);
%MMacro(date='24AUG2020'd,CAT=X8,Ffmt=FF8F.);
%MMacro(date='24AUG2020'd,CAT=X9,Ffmt=FF9F.);
%MMacro(date='24AUG2020'd,CAT=X10,Ffmt=FF10F.);


 until

%MMacro(date='05SEP2020'd,CAT=X1,Ffmt=FF1F.);
%MMacro(date='05SEP2020'd,CAT=X2,Ffmt=FF2F.);
%MMacro(date='05SEP2020'd,CAT=X3,Ffmt=FF3F.);
%MMacro(date='05SEP2020'd,CAT=X4,Ffmt=FF4F.);
%MMacro(date='05SEP2020'd,CAT=X5,Ffmt=FF5F.);
%MMacro(date='05SEP2020'd,CAT=X6,Ffmt=FF6F.);
%MMacro(date='05SEP2020'd,CAT=X7,Ffmt=FF7F.);
%MMacro(date='05SEP2020'd,CAT=X8,Ffmt=FF8F.);
%MMacro(date='05SEP2020'd,CAT=X9,Ffmt=FF9F.);
%MMacro(date='05SEP2020'd,CAT=X10,Ffmt=FF10F.);


 

 

2 REPLIES 2
Kurt_Bremser
Super User

Two nested %DO loops:

%macro outer;
%do loopdate = %sysfunc(inputn(20200823,yymmdd8.)) %to %sysfunc(inputn(20200905,yymmdd8.));
  %do i = 1 %to 10;
    %mmacro(date=&loopdate.,cat=X&i.,ffmt=FF&i.F.);
  %end;
%end;
%mend;
%outer
unison
Lapis Lazuli | Level 10

Another approach -- using the data step routine to loop through the date range and inner-loop (1-10) range and call the execute routine to invoke the macro.

*Table to show the macro parameters;
proc sql;
	create table temp 
	(
		DATE char(12),
		CAT char(3),
		FFmt char(6)
	);
quit;

*Dummy macro definition that inserts the macro parameters into the temp table;
%macro MMacro(date=,CAT=,Ffmt=);
	proc sql;
		insert into temp
		values ("&date","&cat","&ffmt");
	;quit;
%mend;

*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;
*User input macro variables;
%let sdt = 23AUG2020;
%let edt = 05SEP2020;
%let inner_loops = 10;

*Loop through date range and number of inner_loops;
data _null_;
	format i date9.;
	day_cnt=0;

	do until(i>="&edt"d);
		n_cnt=1;
		i=intnx('day', "&sdt"d, day_cnt);

		do j=1 to &inner_loops;
			str=cats('%MMacro(date=', cats("'", put(i, date9.), "'d"), ',CAT=X', n_cnt,',Ffmt=FF', n_cnt, 'F.);'); *<---- Build the command;
			call execute(str); *<---- Execute the "str" command;
			n_cnt+1;
		end;
		day_cnt+1;
	end;
run;

*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;
*Check all of the macro parameter values that were used;
proc print data=temp;
run;
-unison

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 2 replies
  • 720 views
  • 0 likes
  • 3 in conversation