From: karma on
Dear SAS-L,

When creating Clinical reports that are summarized by unique subjects
by levels, adverse events summaries for example, I have yet to find a
flexible solution.

My best attempt so far is given below, but I would like to not have to
call a macro as many times as the number of levels I want it
summarized to. Sure I could macro loop through the by variables, but
I'm hoping that someone can offer a more elegant suggestion :-)

Thanks in advance.


*s=subject ;
*t=treatment ;
data have ;
input (a b) (:$1.) t s ;
cards ;
A A 1 1
A A 1 2
A A 1 2
A A 2 2
A B 1 3
A B 2 1
B A 1 3
;;;;
proc print;run ;
%macro countDistinct(dsetin=have, dsetout=na, by=t a b c, uniq=s) ;

proc sort data=&dsetin out=&dsetout nodupkey ;
by &by &uniq ;
run ;
proc summary data=&dsetout nway completetypes classdata=have ;
class &by ;
output out = &dsetout(rename=(_freq_=count) drop=_type_);
run ;

%mend countDistinct ;

%countDistinct(dsetin=have, dsetout=a, by=t a b) ;
%countDistinct(dsetin=have, dsetout=b, by=t a ) ;
%countDistinct(dsetin=have, dsetout=c, by=t ) ;

data want ;
set a b c ;
a = coalescec(a,'*') ;
b = coalescec(b,'-') ;
run ;
proc sort data=want ;
by a b ;
run;
proc transpose data=want out=trans(drop=_:) prefix=Group_;
id t ;
by a b ;
var count ;
run ;
proc print; run ;

output:

Obs a b Group_1 Group_2

1 * - 3 2
2 A - 3 2
3 A A 2 1
4 A B 1 1
5 B - 1 0
6 B A 1 0
7 B B 0 0