proc format;
value $fmt(default=80)
'Very Heavy (> 25)','Moderate (6-15)','Light (1-5)'=' other'; /*add a white blank before 'other' is to make 'other' appeared at first place in PERCENT_MAIN dataset*/
run;
data have;
set sashelp.heart(keep=Smoking_Status rename=(Smoking_Status=type) where=(type is not missing));
category=put(type,$fmt.);
run;
proc freq data=have noprint order=internal;
table category/out=percent_main; /*get main pie percent*/
table category*Type/out=percent_all; /*get sub pie percent*/
run;
/*process main pie*/
data main_temp;
set percent_main;
PERCENT=0.01*PERCENT;
if _n_=1 then percent_cum=PERCENT/2; /*when category='other'*/
else percent_cum+PERCENT;
keep category percent_cum PERCENT;
format PERCENT percent8.2;
run;
data main_pie;
set main_temp;
pi=constant('pi');
lag_percent_cum=lag(percent_cum);
label=catx('|',category,put(PERCENT,percent8.2));
id=_n_;x=0;y=0;output;
if _n_=1 then do;
do theta=-2*pi*percent_cum to 2*pi*percent_cum by 0.001;
x=cos(theta);y=sin(theta); output;
end;
end;
else do;
do theta=2*pi*lag_percent_cum to 2*pi*percent_cum by 0.001;
x=cos(theta);y=sin(theta); output;
end;
end;
keep category id x y PERCENT label ;
run;
/*process sub pie*/
proc sql;
create table sub_temp as
select type,0.01*PERCENT as PERCENT format=percent8.2,
sum(calculated PERCENT) as sum_percent,calculated sum_percent/2 as half_percent
from percent_all
where strip(category)='other';
quit;
data sub_pie;
set sub_temp(rename=(type=category) );
pi=constant('pi');
id=10000+_n_;
cum_percent+PERCENT;
lag_cum_percent=coalesce(lag(cum_percent),0);
label=catx('|',category,put(PERCENT,percent8.2));
do theta=-2*pi*half_percent+2*pi*lag_cum_percent to -2*pi*half_percent+2*pi*cum_percent by 0.001;
x=0.5*cos(theta);y=0.5*sin(theta); output;
end;
do theta=-2*pi*half_percent+2*pi*cum_percent to -2*pi*half_percent+2*pi*lag_cum_percent by -0.001;
x=cos(theta);y=sin(theta); output;
end;
keep category id x y PERCENT label ;
run;
/*Plot pie of pie chart*/
data pie;
length category $ 200;
set main_pie(where=(id ne 1)) sub_pie ;
run;
ods graphics/noborder ANTIALIAS ANTIALIASMAX=10000000 width=800px height=600px;
proc sgplot data=pie aspect=1 noautolegend noborder;
polygon id=id x=x y=y/group=category label=label labelattrs=(color=black size=12) fill nooutline dataskin=sheen
LABELLOC=INSIDEBBOX LABELPOS=CENTER name='x' splitchar='|' ;
xaxis display=none;
yaxis display=none;
run;
... View more