%let name=cancer_intervention;
filename odsout '.';


data my_data;
input condition $ 1-20
  Behavioral Biological Device Drug Genetic Procedure Radiation 
  actual_duration expected_duration;
datalines;
Anal Cancer           . .50 . .75 . .50 . 4.1 3.7
Bladder Cancer        .333 .667 .444 .889 0 .778 .222 4.6 3.7
Brain Cancer          .143 .429 .143 1.00 . .714 .429 2.8 2.4
Breast Cancer         .778 .222 .333 1.00 0 .666 .444 4.8 4.0
Cervical Cancer       .500 .250 .25 .875 . .75 .375 4.6 3.8
Colon Cancer          .778 .444 .333 1 .111 .556 0 3.7 3.1
Gastric Cancer        .556 .333 .555 1 .111 .778 .222 3.9 3.2
Head and Neck Cancer  .556 .333 .111 .889 0 .444 .667 4.7 3.8
Leukemia              .444 .778 .111 .889 .333 .556 0 4.9 4.1
Lung Cancer           .222 .444 .333 1 .111 .778 .667 4.0 3.3
Melanoma              .778 .778 .778 .889 0 .444 .111 4.6 4.3
Myelofibrosis         . . . 1 . .333 . 4.3 3.3
Ovarian Cancer        .556 .778 .222 1 .333 .667 .111
Pancreatic Cancer     .111 .667 .444 .889 .333 .778 .222 3.9 3.2
Prostate Cancer       .444 .556 .222 1 0 .667 .778 4.2 3.8
Rectal Cancer         .375 .125 .25 .875 0 .75 .625 5.0 3.9
Thyroid Cancer        .375 .125 .375 1 . .875 .5 3.8 2.9
;
run;

proc sort data=my_data out=my_data;
by descending condition;
run;
data my_data; set my_data;
condition_number=_n_;
run;

proc transpose 
 data=my_data (drop=actual_duration expected_duration) 
 out=tran_data (rename=(_name_=intervention col1=percentile_of_trials));
by condition condition_number notsorted;
run;
data tran_data; set tran_data;
length id $100;
id=trim(left(condition))||':'||trim(left(intervention));
format percentile_of_trials percent7.1;
run;

/* controls aspect ratio of colored boxes */
%let x_inc=3.8;

data my_map; set my_data;
length id $100;

intervention='Behavioral';
id=trim(left(condition))||':'||trim(left(intervention));
x=1*&x_inc; y=condition_number; output;
x=x+&x_inc; output;
y=y+1; output;
x=x-&x_inc; output;

intervention='Biological';
id=trim(left(condition))||':'||trim(left(intervention));
x=2*&x_inc; y=condition_number; output;
x=x+&x_inc; output;
y=y+1; output;
x=x-&x_inc; output;

intervention='Device';
id=trim(left(condition))||':'||trim(left(intervention));
x=3*&x_inc; y=condition_number; output;
x=x+&x_inc; output;
y=y+1; output;
x=x-&x_inc; output;

intervention='Drug';
id=trim(left(condition))||':'||trim(left(intervention));
x=4*&x_inc; y=condition_number; output;
x=x+&x_inc; output;
y=y+1; output;
x=x-&x_inc; output;

intervention='Genetic';
id=trim(left(condition))||':'||trim(left(intervention));
x=5*&x_inc; y=condition_number; output;
x=x+&x_inc; output;
y=y+1; output;
x=x-&x_inc; output;

intervention='Procedure';
id=trim(left(condition))||':'||trim(left(intervention));
x=6*&x_inc; y=condition_number; output;
x=x+&x_inc; output;
y=y+1; output;
x=x-&x_inc; output;

intervention='Radiation';
id=trim(left(condition))||':'||trim(left(intervention));
x=7*&x_inc; y=condition_number; output;
x=x+&x_inc; output;
y=y+1; output;
x=x-&x_inc; output;

run;


proc format;
value ranges
 1 = "N/A"
 2 = "= 0%"
 3 = "<= 20%"
 4 = "<= 40%"
 5 = "<= 60%"
 6 = "<= 80%"
 7 = "> 80%"
;
run;

data tran_data; set tran_data;
format legend_bin ranges.;
if percentile_of_trials=. then legend_bin=1;
else if percentile_of_trials=0 then legend_bin=2;
else if percentile_of_trials<=.20 then legend_bin=3;
else if percentile_of_trials<=.40 then legend_bin=4;
else if percentile_of_trials<=.60 then legend_bin=5;
else if percentile_of_trials<=.80 then legend_bin=6;
else if percentile_of_trials<=1.00 then legend_bin=7;
run;

proc sql;
create table anno_column_headers as
select unique intervention as text, avg(x) as x, max(y)+.7 as y
from my_map
group by intervention;
quit; run;
data anno_column_headers; set anno_column_headers;
xsys='2'; ysys='2'; hsys='3'; when='a';
function='label'; position='5'; 
run;

proc sql;
create table anno_row_headers as
select unique condition as text, min(x)-.25 as x, avg(y) as y
from my_map
group by condition;
quit; run;
data anno_row_headers; set anno_row_headers;
xsys='2'; ysys='2'; hsys='3'; when='a';
function='label'; position='4'; 
run;

data anno_all; 
length text $100;
set anno_column_headers anno_row_headers;
run;


data tran_data; set tran_data;
length my_html $300;
my_html='title='||quote(
 trim(left(condition))||'0d'x||
 trim(left(intervention))||'0d'x||
 trim(left(put(percentile_of_trials,percent7.1)))
 );
run;


goptions device=png;
goptions noborder;

ODS LISTING CLOSE;
ODS HTML path=odsout body="&name..htm" 
 (title="Cancer Intervention Research") 
 options(pagebreak='no') style=sasweb;

goptions gunit=pct ftitle='albany amt/bold' ftext='albany amt' htitle=16pt htext=9pt ctext=gray33;

title1 ls=1.5 "Cancer Intervention Research";
title2 h=5pct ' ';
title3 h=20pct a=90 ' ';
title4 h=15pct a=-90 ' ';

footnote "Proof-of-concept, using made-up data";

pattern1 v=m4n45 c=grayaa;
pattern2 v=s c=white;
pattern3 v=s c=cxfee5d9;
pattern4 v=s c=cxfcae91;
pattern5 v=s c=cxfb6a4a;
pattern6 v=s c=cxde2d26;
pattern7 v=s c=cxa50f15;

legend1 label=(position=top j=c 'Percent of trials' j=c 'using intervention') 
 across=1 position=(middle right) offset=(7,0) mode=share
 value=(justify=left) shape=bar(.15in,.15in) order=descending;

goptions xpixels=900 ypixels=425;
proc gmap data=tran_data map=my_map anno=anno_all;
id id;
choro legend_bin / discrete midpoints=(1 2 3 4 5 6 7)
 legend=legend1
 coutline=grayaa
 html=my_html
 des='' name="&name";
run;

data my_data; set my_data;
length my_html $3200;
my_html='title='||quote(
 trim(left(condition))||'0d'x||
 'Actual duration: '||trim(left(actual_duration))||' years'||'0d'x||
 'Expected duration: '||trim(left(expected_duration))||' years'||'0d'x||
 'difference: '||trim(left(put((actual_duration-expected_duration),comma8.2)))
 );
if condition='Head and Neck Cancer' then condition='Head and Neck\Cancer';
fake_legend='Average Actual\Trial Duration';
run;


data anno_bars; set my_data;
length function $8 color $8;
when='a'; 

xsys='2'; xc=condition; ysys='2'; y=expected_duration; function='move'; output;
xsys='7'; x=-1.9; function='move'; output;
x=0; y=actual_duration; function='draw'; size=0.1; color="blue"; output;
xsys='2'; xc=condition; y=expected_duration; function='move'; output;
xsys='7'; x=-1.9; function='move'; output;
function='cntl2txt'; output;
x=0; y=expected_duration+(actual_duration-expected_duration)/2; 
 function='label'; position='6'; hsys='d'; size=7; color="blue"; 
 text='  '||trim(left(put((actual_duration-expected_duration),comma8.1))); output;

hsys='3';
xsys='2'; xc=condition; y=expected_duration; function='move'; output;
xsys='7'; x=3*-1; function='move'; output;
xsys='7'; x=3*2; function='draw'; size=1.0; color="cxdc5fbd"; output;

run;

data anno_legend_extras;
length function $8 color $8 text $100;
xsys='3'; ysys='3'; hsys='3'; when='a';
function='move'; x=86; y=53; output;
function='draw'; y=y-4; size=0.1; color="blue"; output;
function='move'; x=85; y=42; output;
function='draw'; x=x+2; size=1.0; color="cxdc5fbd"; output;
function='label'; size=.; color=''; position='6';
x=87.5;
y=52; text='difference'; output;
y=42; text='Average Expected'; output;
run;


pattern1 v=s c=cxd7d5c5;

axis1 color=grayaa label=(angle=90 c=gray33 'Trial Duration (in Years)') 
 value=(c=gray33) minor=none offset=(0,0);

axis2 color=grayaa label=none value=(angle=90 c=gray33) split='\' offset=(3,3);

legend1 position=(right middle) mode=share value=('Average Actual')
 label=none shape=bar(.15in,.15in) offset=(5,10);

title1 h=2 ' ';
title2 a=90 h=10 ' ';
title3 a=-90 h=15 ' ';

goptions xpixels=900 ypixels=375;
proc gchart data=my_data anno=anno_bars;
vbar condition / type=sum sumvar=actual_duration
 raxis=axis1 maxis=axis2 noframe
 subgroup=fake_legend legend=legend1 anno=anno_legend_extras
 width=3.0 space=1.7
 coutline=grayaa
 html=my_html
 des='' name="&name._bar";
run;

quit;
ODS HTML CLOSE;
ODS LISTING;
