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.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.