How to merge non-empty tables only? skip empty tables.
In what way are empty datasets causing a problem during your merge operation. In the following example, merging with an empty dataset isn't a problem, except for the presence of an empty column
data a;
id=1; x=1; output;
id=1; x=2; output;
run;
/* Empty dataset */
data b;
length id x y 8;
stop;
run;
data c;
id=1; z=3; output;
id=1; z=4; output;
run;
data d;
merge a b c;
by id;
run;
proc print data=d noobs; run;
id x y z 1 1 . 3 1 2 . 4
In what way are empty datasets causing a problem during your merge operation. In the following example, merging with an empty dataset isn't a problem, except for the presence of an empty column
data a;
id=1; x=1; output;
id=1; x=2; output;
run;
/* Empty dataset */
data b;
length id x y 8;
stop;
run;
data c;
id=1; z=3; output;
id=1; z=4; output;
run;
data d;
merge a b c;
by id;
run;
proc print data=d noobs; run;
id x y z 1 1 . 3 1 2 . 4
Thank you, PG. There is no issue to merge empty tables. I just don't want to keep columns from empty tables.
I have created a simple macro. You can use it.
%macro tablesize(tablename=);
proc sql noprint;
select count(*) into :cnt from &tablename.;
quit;
%if &cnt. ne 0 %then %do;
%let &tablename.=&tablename.;
%end;
%else %do;
%let &tablename.=;
%end;
%mend tablesize;
%tablesize(tablename=A);
%tablesize(tablename=B);
%tablesize(tablename=C);
data d;
merge &A. &B. &C.;
by common_var;
run;
/*Which of the table is empty its corresponding macro will be initialized to null
so they wont be merged in the data step as the macro resoves to null
Remember to call macronames insted of tablenames.
Remember Not to have any macro variable same name as tablenames*/
Please let us know if it worked for you.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.