Hi @_Sas_Beginner_
I'm approaching this from these perspectives
Using named macro parameters rather than positional
Reding the source data set only once, while producing multiple output data sets
Providing the flexibility to explicitly pick your min-max months
%MACRO EXTRACT(p_inDsName=, p_year=, p_minMonth=1, p_maxMonth=12);
%LOCAL l_i;
DATA
/* Dynamically construct the output data set names */
%do l_i=&p_minMonth %to &p_maxMonth; _&p_year.%sysfunc(putn(&l_i,z2.)) %end;
;
/* Using DOW Loop for selective iterative processing */
DO _n_=1 by 1 UNTIL (last.VAR2);
/* Filter the data by specified year */
SET &p_inDsName(where=(year=&p_year));
BY VAR2;
SUM+VAR3;
MEAN=SUM/_N_;
/* Dynamically construct the output statements */
%let l_i=&p_minMonth;
if (put(MONTH,month.)=&l_i) then output _&p_year.%sysfunc(putn(&l_i,z2.));
%do l_i=%eval(&p_minMonth+1) %to &p_maxMonth;
else if (put(MONTH,month.)=&l_i) then output _&p_year.%sysfunc(putn(&l_i,z2.));
%end;
END;
KEEP VAR2 VAR3 SUM MEAN;
RUN;
%MEND EXTRACT;
options mprint; /* Display in the log the SAS statements being processed */
/* Ensure data set properly sorted */
%let g_srcDsName= first_2017; proc sort data=&g_srcDsName;
by var2 year;
run;
%EXTRACT(p_inDsName=&g_srcDsName, p_year=2017, p_minMonth=1, p_maxMonth=12);
Hope this helps,
Ahmed
... View more