BookmarkSubscribeRSS Feed
boomboomtanner
Calcite | Level 5

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!

2 REPLIES 2
ghosh
Barite | Level 11
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;
Kurt_Bremser
Super User

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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 868 views
  • 3 likes
  • 3 in conversation