BookmarkSubscribeRSS Feed
skygold16
Fluorite | Level 6

Hello All,

I have some kind of weird requirement, but have to fulfill.

I want to merge all the tables of the library.

1. I don't know how many tables will be available.

2. Table structure for all the tables in library is same.

3. I want to use coalesce function to find out which is the non-missing variable and that value I have to pick up for a variable of output dataset.

4. Yes, key is same.

Can anyone please help at earliest?

Thanks,

Hatshit

1 REPLY 1
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Well, its a bit vague but what you will need to do is to get a list from SASHELP.VTABLE with a where clause of your libname.  This will give you a list of the datasets in that libname.  You can then use that call execute a macro which will sort and merge that onto a base table.  E.g

%macro add (ds);

     proc sort data=&ds. out=temp;

          by idvar;

     run;

     data base;

          merge base temp;

          by idvar;

     run;         

%mend add;

data list_of_tables (keep=libname memname);

     set sashelp.vtable;

     where libname="WORK";

     if _n_=1 then do;

          call execute('data base; set '||strip(libname)||"."||strip(memname)||'; run;'); /* This takes the first dataset as the base */

     end;

     else call execute('%add('||strip(libname)||"."||strip(memname)||';');

run;

Of course this assumes a lot of things - datasets are the same, have the same idvar, there is more than 1 etc.  You could build an SQL statement using call execute, this is limited to 256 tables though.  As for you coalesce, well, in the merge I would rename the temp variables, and then compare.

Catch up on SAS Innovate 2026

Nearly 200 sessions are now available on demand with the SAS Innovate Digital Pass.

Explore Now →
What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 1 reply
  • 1654 views
  • 3 likes
  • 2 in conversation