Download compcnt.sas compcnt.sasSubmit a comment

%macro compcnt (data1, data2, by);

	%local I BVS N;

	%* Caveat: %makelist newmvarp= has the side-effect of creating global macro variables BV1-BVN;

	%makelist (&by, BVS, N, sep=%str(,), newmvarp=BV);

	proc sql;
	  create table compcnt as
	  select
		    "&data1" as DS1
		  , _COUNT1_
		  , _COUNT2_
		  , "&data2" as DS2
%do i=1 %to &N;
	    , ONE.&&BV&I
%end;
	  from
	    (select &BVS, count(*) as _COUNT1_
	     from &data1
	     group by &BVS
	    ) ONE
	    ,
	    (select &BVS, count(*) as _COUNT2_
	     from &data2
	     group by &BVS
	    ) TWO
	  where
	  	ONE._COUNT1_ ne TWO._COUNT2_
%do i=1 %to &N;
 			and ONE.&&BV&I = TWO.&&BV&I
%end;
  	;
	quit;

%mend;

/*
data x;
  set sashelp.class;
  where age=13;
  output;
  output;
run;

proc sql;
  create table y as
  select * from sashelp.class
  where age=13;
quit;

%compcnt (x, y, age name)
*/