Or if PROC SQL is your flavour (this is the 2nd post - accidentally submitted the first post and then deleted it): proc sql ; create table want as select cd.* , coalesce ( q1.primarytable1, 0 ) as primarytable1 , coalesce ( q2.primarytable2, 0 ) as primarytable2 , coalesce ( q1.secondarytable1, 0 ) as secondarytable1 , coalesce ( q2.secondarytable2, 0 ) as secondarytable2 from /* create a set of unique codes from both datasets */ ( select distinct coalesce ( t1.code , t2.code) as code, coalesce ( t1.descrip, t2.descrip ) as descrip from table1 t1 full outer join ( select code, descrip from table2 ) t2 on t1.code = t2.code ) cd /* add up categories from table1 */ left join ( select code , sum ( case when category='primary' then 1 else 0 end ) as primarytable1 , sum ( case when category='secondary' then 1 else 0 end ) as secondarytable1 from table1 group by code ) q1 on q1.code = cd.code /* add up categoris from table2 */ left join ( select code , sum ( case when category='primary' then 1 else 0 end ) as primarytable2 , sum ( case when category='secondary' then 1 else 0 end ) as secondarytable2 from table2 group by code ) q2 on q2.code = cd.code order by code ; quit;
... View more