Frequent merge code replaced by macro

From sasCommunity
Jump to: navigation, search

The merge process has some standard steps that are annoying to repeat in the work I do. Here is a merge macro to prep the datasets, merge and generate standard error reports for the outer sets.

by Andrew Peddie

out = out dataset 
in1 = first dataset to merge
in2 = second dataset
bylist = merge variables

Warnings The only issue you will have is the in=a and in=b variables if they are used in your datasets already.

%macro pmerge(out,in1,in2,bylist);

proc sort data = &in1;
	by &bylist;
proc sort data = &in2;
	by &bylist;

data &out &out.E &out.EE;
	merge &in1(in=a) &in2(in=b);
	by &bylist;
if a and b then output &out;
if a and not b then output &out.E;
if b and not a then output &out.EE;
run;

Proc sql;
Title " ERRORS ON MERGE OF &in1 AND &in2 MERGED BY &bylist. SAS CODE &version";
Title2 " I.E THOSE IN &in1 AND NOT IN &in2 DATASET &out.E";
select count(*) 
from &out.E
;

proc sql;
Title " ERRORS ON MERGE OF &in1 AND &in2 MERGED BY &bylist. SAS CODE &version"; 
Title2 " I.E THOSE IN &in2 AND NOT IN &in1 DATASET &out.EE";
select count(*) 
from &out.EE
;
quit;

Title2 ;
%mend pmerge;