Hi team,
I am looking to create one master table which contains 13 months worth of data.
Currently the table names are as such:
XXX_AUG22
XXX_JUL22
XXX_JUN22
... etc
Is there a way to append these tables using a macro loop by combining this month's data + the last 12 months?
Appreciate your help!
data lstmonth; do i=0 to 13; dates=cats('XXX_',put(intnx('month',today(),-i,'s'),monyy.)); output; end; run; proc sql noprint; select dates into :datlst separated by ' ' from lstmonth ; quit;
%put &=datlst;
/* you will get this list:
DATLST=
XXX_AUG22 XXX_JUL22 XXX_JUN22 XXX_MAY22 XXX_APR22 XXX_MAR22 XXX_FEB22 XXX_JAN22 XXX_DEC21 XXX_NOV21 XXX_OCT21 XXX_SEP21 XXX_AUG21 XXX_JUL21*/
data combined;
set &datlst
indsname = source; /* the INDSNAME= option stores the dataset name */
run;
As a very big hint for the future: never use date strings like these in dataset names or similar. Use a YYYYMM string instead.
You can then combine a year simply like this:
data want;
set have_2022:;
run;
In all displays, the datasets will always sort in chronological order.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.