I used macro to retrieve datasets of statistics of a set of variables. Now that I want to print a report, I am wondering if there is some trick to minimise the lines of the code without having to repeat proc report statement. here is my whole code proc format;
value grp 1='Group 1' 2='Group 2' 3='Total';
value $sex 'F'='Female' 'M'='Male';
value origin 1 ='Belgium' 2='other';
run;
/*get data and create missing variables*/
data Class1;
set sashelp.class;
group = rantbl(12345,.5);
origin = rantbl(12345,.5);
_height=height*2.54;
_Weight=weight/2.2;
bmi = (weight*703) / height**2;
attrib age format=F3.0 label='Age (years)';
attrib _height format=F7.1 label='Height (cm)';
attrib _weight format=F7.1 label='Weight (kg)';
attrib sex format=$sex. label='Gender';
attrib origin format=origin. label='Country of birth';
attrib bmi format=F7.2 label='BMI (kg/m**2)';
attrib group format=grp. label='Group';
run;
/* create total variable*/
data Class1Tot;
set Class1;
attrib ctrt length=$1 label='Character grp';
do group= group, 3;
ctrt = substr('ABC',group,1);
output;
end;
run;
%Macro tablestat(dataset=,Var1=,char=);
proc summary data=&dataset completetypes;
Class group/preloadfmt;
var &Var1;
output out=stat median=med mean=mn std=StD q1=q1 q3=q3 n=n max=max min=min;
run;
data stat;
set stat;
if group=. then delete;
mnx=compress(min)!!' - '!! compress(max);
q12=compress(q1)!!' - '!! compress(q3);
mnsd= compress(put(mn,8.1))!!' ('!!compress(put(std,8.1))!!')';
nx=put(_freq_,8.0);
medx=put(med,8.0);
drop q1 q3 mn std _freq_ med n max min _type_;
run;
data t1;
set stat;
array b(*) $ nx mnsd medx q12 mnx;
do i=1 to 5;
c=b(i);
output;
end;
drop nx mnsd medx q12 mnx ;
run;
proc sql;
create table C1 as
select a.i,a.c,b.c as c2
from T1(where =(group EQ 1)) as a left join T1(where =(group EQ 2)) as b
on a.i=b.i;
create table &var1 as
select d.*,e.c as c3
from c1 as d left join T1(where =(group EQ 3)) as e
on d.i=e.i;
quit;
data &var1;
set &var1;
length char $200.;
char="&char";
run;
%mend;
%tablestat(dataset=Class1Tot,var1=_height,char=Height (cm));
%macro category(data=,var=,char=);
ods listing close;
proc freq data=&data;
tables &var*group/norow nopercent;
ods output CrossTabFreqs=discret(where=(_type_ in('11')));
run;
ods listing;
data T1;
set discret;
nx=compress(put(frequency,8.0))!!'('!!compress(put(Colpercent,8.1))!!'%)';
drop _type_ table _table_ Missing Frequency Colpercent;
run;
proc sql;
create table T2 as
select a.&var,a.nx, b.nx as c1
from T1(where=(group EQ 1)) as a left join T1(where=(group EQ 2)) as b
on a.&var=b.&var;
create table &var as
select d.*,e.nx as c2
from T2 as d left join T1(where=(group EQ 3)) as e
on d.&var=e.&var;
quit;
data &var;
set &var(rename=(nx=c));
length char $200.;
char="&char";
run;
%mend;
%category(data=Class1tot,var=origin,char=country of birth);
/*-----Display---*/ Thanks a lot
... View more