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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 2254 views
  • 0 likes
  • 3 in conversation