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

How to merge non-empty tables only? skip empty tables.

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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
PG

View solution in original post

4 REPLIES 4
PGStats
Opal | Level 21

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
PG
invitro
Calcite | Level 5

Thank you, PG. There is no issue to merge empty tables. I just don't want to keep columns from empty tables.

Satish_Parida
Lapis Lazuli | Level 10

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.

invitro
Calcite | Level 5
Thank you, Satish_Parida. It should work.

sas-innovate-wordmark-2025-midnight.png

Register Today!

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.


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
  • 4 replies
  • 2613 views
  • 0 likes
  • 3 in conversation