BookmarkSubscribeRSS Feed
Timbim
Obsidian | Level 7

Hi Community,

 

I'm here seeking help for a macro to merge tables with similar table structures.

 

I've written the below code but I believe a macro would be easy.

 

Data Accounts_FY18;

set Account _201801

      Account _201802

     Account _201803

    Account _201804

     Account _201805

     Account _201806

    Account _201807

    Account _201808

   Account _201809

   Account _201810

  Account _201811

  Account _201812;

run;

     Appreciate your help.

 

Kind regards

3 REPLIES 3
andreas_lds
Jade | Level 19

Well, i would be a good starter to discuss why those 12 datasets have been created at all, that seems to be bad design.

 

Now the good news: you don't need macro-code!

 

data Accounts_FY18;
    set Account_2018:;
run;

This causes all datasets named like "Account_2018*" to be appended.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Just to add to @andreas_lds  great answer, use correct terminology.  You are not "merging" data, you are appending data.  There is a big difference.

I would also agree strongly with the "why those 12 datasets", as this may also impact your processing.  If these datasets are large, then you may need to use the append procedure rather than the datastep approach, example:

data a1;
  a=1;
run;
data a2;
  a=2;
run;
data a3;
  a=3;
run;

data _null_;
  set sashelp.vtable (where=(libname="WORK" and char(memname,1)="A"));
  if _n_=1 then call execute(cats('data want; set work.',memname,';run;'));
  else call execute(cats('proc append base=want data=work.',memname,';run;'));
run;
Ksharp
Super User
Data Accounts_FY18;

set Account _201801 - Account _201812;

run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 650 views
  • 7 likes
  • 4 in conversation