BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
eh51
Calcite | Level 5

My SAS program creates 10, more or less, data sets. I would like to merge them with the result of a final want table containing each of the 'results...' variables of each have table. Each dataset has a 'cust' variable that would be the 'by' variable in a data merge setup, or where = in SQL.

My problem is I might expand the number of sets or reduce them.

I can query dictionary and get a nice list of the tables I created.  How do I use those in a merge by or sql from?

Two example tables are below. 

 

 

proc sql ;
  create table tbl_haves as 
  select memname
  from dictionary.tables
  where libname = 'WORK'
    and memname like 'STK%PM' 
  ;
quit;

 

 

have_tbl_12ab

 

casecustresults12ab
111271-2217385
101279-2231597
91292-2254441
81305-2276959
71318-2299131
61331-2320933
51344-2342338
41357-2363316
31370-2383831
21383-2403842
11396-2423304

 

 

have_tbl_45cv

 

casecustresults45cv
1541271-2291000
1531279-2317000
1521292-2341000
1511305-2364000
1501318-2388000
1491331-2413000
1481344-2438000
1471357-2460000
1461370-2484000
1451383-2508000
1441396-2530000

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

To merge multiple dataset just use the MERGE statement.

data want;
  merge ds1 ds2 ds3 ;
  by customer;
run;

If you put the list of dataset names into a macro variable you can use that to generate the code.

%let dslist=ds1 ds2 ds3 ;
data want;
  merge &dslist;
  by customer;
run;

You can change your PROC SQL query to generate a macro variable instead of a dataset.

proc sql noprint;
select nliteral(memname) into :dslist separated by ' ' 
  from dictionary.tables
  where libname = 'WORK'
    and memname like 'STK%PM' 
;
quit;

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

To merge multiple dataset just use the MERGE statement.

data want;
  merge ds1 ds2 ds3 ;
  by customer;
run;

If you put the list of dataset names into a macro variable you can use that to generate the code.

%let dslist=ds1 ds2 ds3 ;
data want;
  merge &dslist;
  by customer;
run;

You can change your PROC SQL query to generate a macro variable instead of a dataset.

proc sql noprint;
select nliteral(memname) into :dslist separated by ' ' 
  from dictionary.tables
  where libname = 'WORK'
    and memname like 'STK%PM' 
;
quit;
Astounding
PROC Star

What do you intend to do with the variable CASE, which appears in all data sets, but varies from one data set to the next?

eh51
Calcite | Level 5

I dropped the variable 'case'.  It is not needed, but was good for tracking up until that merge.

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
  • 3 replies
  • 1496 views
  • 2 likes
  • 3 in conversation