You have had a few suggestions of various ways to simplify your code using wildcards etc. - but if you need to do the append with a specific number of increasing dates, your own method may be the right way to do it.
The problem you have with the BASEMON&count variable is, as @Kurt_Bremser remarked, that you put the macro variable name in single quotes, like 'BASEMON&COUNT', this means the data step will try to put data into a macro variable named BASEMON&COUNT and not e.g. BASEMON1 (no resolution of the COUNT variable, so the macro variable name will not be syntactically correct).
But I would probably write the macro somewhat differently, a first version would look like this:
%MACRO JOIN;
%local StartMonth NoofMonths OutData Date Month DD_Month;
%LET StartMonth = 202202; /* a better and more descriptive name than DD */
%let NoofMonths=5; /* we do 1 to 5 and not 0 to 4, is intuitively easier */
%let OutData=TEST;
/* now convert StartMonth to e.g. 01FEB2022 */
%let Date=%sysfunc(input(&StartMonth,YYMMN6.),date9.);
/* Delete output, if exists */
Proc delete data=&outdata;run;
/* and then we loop, proc APPEND will create a new table if BASE does not exist */
%do Month=1 %to &NoofMonths;
%let DD_Month=%sysfunc(put("&Date"d,YYMMN6.));
PROC APPEND BASE =&outdata DATA=DATA_SET_&DD_Month;
RUN;
%let Date=%sysfunc(intnx(MONTH,"&Date"d,1,B),date9.);
%end;
%MEND;
You could also make the DATE macro variable a numeric variable, but I prefer using the DATE9. format, which makes SYMBOLGEN output much easier to understand.
When running for real, you will want to use parameters for your macro, e.g.
%MACRO JOIN(StartMonth,NoofMonths,OutData);
%local Date Month DD_Month;
/* now convert StartMonth to e.g. 01FEB2022 */
%let Date=%sysfunc(input(&StartMonth,YYMMN6.),date9.);
/* Delete output, if exists */
Proc delete data=&outdata;run;
/* and then we loop, proc APPEND will create a new table if BASE does not exist */
%do Month=1 %to &NoofMonths;
%let DD_Month=%sysfunc(put("&Date"d,YYMMN6.));
PROC APPEND BASE =&outdata DATA=DATA_SET_&DD_Month;
RUN;
%let Date=%sysfunc(intnx(MONTH,"&Date"d,1),date9.);
%end;
%MEND;
So you can call it like
%join(202202,5,TEST);
... View more