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

/*
SAS version of chart 2 from:
http://www.datasciencecentral.com/profiles/blogs/visualizations-comparing-tableau-spss-r-excel-matlab
http://api.ning.com/files/LGdhCycw6D9wxJqwMI7lHCzJl9-P*yXVhpXUFFEQK5mCbToMiMXDv9517irQPPEiPn7Q2RgB6TUzU8-Vd0H0*DyJ*y-s1HSr/untitled2.png
*/

data my_data;
input question $ 1-80;
question_order+1;
input country $ 1-3 bin1 bin2 bin3 bin4; output;
input country $ 1-3 bin1 bin2 bin3 bin4; output;
input country $ 1-3 bin1 bin2 bin3 bin4; output;
datalines;
I read only if I have to.
USA .20 .30 .30 .20
MEX .24 .35 .31 .10
CAN .26 .35 .20 .19
Reading is one of my favorite hobbies.
USA .30 .40 .20 .10
MEX .20 .32 .36 .12
CAN .28 .34 .22 .16
I like talking about books with other people.
USA .25 .34 .30 .11
MEX .20 .33 .35 .12
CAN .25 .32 .30 .13
I find it hard to finish books.                 
USA .29 .40 .25 .06
MEX .25 .36 .33 .06
CAN .30 .42 .21 .07
I feel happy if I receive a book as a present.  
USA .30 .32 .30 .08
MEX .18 .25 .40 .17
CAN .25 .25 .30 .20
For me, reading is a waste of time.             
USA .35 .39 .16 .10
MEX .44 .44 .08 .04
CAN .38 .38 .14 .10
I enjoy going to the bookstore or a library.    
USA .25 .22 .33 .20
MEX .22 .32 .40 .06
CAN .20 .28 .32 .20
I read only to get information that I need.     
USA .18 .35 .40 .07
MEX .14 .30 .46 .10
CAN .20 .41 .30 .09
I cannot sit still and read for more than a few minutes.
USA .35 .36 .20 .09
MEX .34 .43 .20 .03
CAN .40 .36 .16 .08
I like to express my opinions about books I have read.  
USA .20 .29 .32 .19
MEX .10 .27 .41 .22
CAN .17 .30 .33 .20
I like to exchange books with my friends.               
USA .30 .36 .20 .14
MEX .22 .30 .30 .18
CAN .28 .32 .30 .10
run;

data my_data; set my_data;
if country='USA' then bar_order=1;
if country='MEX' then bar_order=2;
if country='CAN' then bar_order=3;
run;

data plot_data; set my_data;
length hovertext $300;
hovertext='title='||quote(
 trim(left(question))||'0d'x||
 'Country: '||trim(left(country))||'0d'x||
 '------------------------'||'0d'x||
 trim(left(put(bin1,percent7.0)))||' Strongly Disagree'||'0d'x||
 trim(left(put(bin2,percent7.0)))||' Disagree'||'0d'x||
 trim(left(put(bin3,percent7.0)))||' Agree'||'0d'x||
 trim(left(put(bin4,percent7.0)))||' Strongly Agree'
 );
bin=1; value=bin1; output;
bin=2; value=bin2; output;
bin=3; value=bin3; output;
bin=4; value=bin4; output;
run;

proc sql noprint;
create table control as
select unique question_order as start, question as label
from plot_data;
quit; run;
data control; set control;
fmtname='q_fmt';
type='N';
run;
proc format lib=work cntlin=control;
run;

proc sql noprint;
create table control as
select unique bar_order as start, country as label
from plot_data;
quit; run;
data control; set control;
fmtname='bar_fmt';
type='N';
run;
proc format lib=work cntlin=control;
run;

proc format;
value bin_fmt
1="Strongly Disagree"
2="Disagree"
3="Agree"
4="Strongly Agree"
;
run;


proc sql noprint;
create table anno_questions as
select unique question_order as group, question as text, bar_order as midpoint
from plot_data
where country='USA';
quit; run;

data anno_questions; set anno_questions;
length function $8 color $8 text $300;
hsys='3'; when='a';
position='2'; 
xsys='1'; x=50; ysys='2'; 
function='move'; output;
xsys='7'; ysys='7'; 
x=0; y=1.15; function='move'; output;
function='cntl2txt'; output;
function='label'; size=.; position='5'; color="black"; cbox="white"; 
text=trim(left(group))||'. '||trim(left(text));
output;
run;

data anno_summary; set my_data;
length function $8 color $8;
xsys='1'; ysys='2'; hsys='3'; when='a';
group=question_order;
midpoint=bar_order;
function='label'; position='5'; 
x=2.5;  text=trim(left(put((bin1+bin2)*100,comma8.0))); color="cxce618a"; output;
x=97.5; text=trim(left(put((bin3+bin4)*100,comma8.0))); color="cxac9478"; output;
run;


goptions device=png;
goptions xpixels=700 ypixels=1500;
goptions noborder;

ODS LISTING CLOSE;
ODS HTML path=odsout body="&name..htm" 
 (title="Reading survey results") style=htmlblue;

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

pattern1 v=s c=cxb03060;
pattern2 v=s c=cxd797af;
pattern3 v=s c=cxc4b9aa;
pattern4 v=s c=cx8b7354;

axis1 style=0 label=none value=(font='albany amt/bold') offset=(1,1);
axis2 style=0 label=none value=none offset=(1,1);
axis3 style=0 label=none order=(0 to 1 by .25) value=(color="gray77")
 major=none minor=none offset=(4,4);

legend1 position=(top center) label=none across=4 value=(justify=left) 
 shape=bar(.15in,.15in) offset=(5,0);

title1 ls=1.5 "Reading Survey Responses:";
proc gchart data=plot_data anno=anno_questions;
format value percent7.0;
format question_order q_fmt.;
format bar_order bar_fmt.;
format bin bin_fmt.;
hbar bar_order / discrete type=sum sumvar=value nostats
 subgroup=bin legend=legend1
 group=question_order
 maxis=axis1 gaxis=axis2 raxis=axis3 noframe
 space=0 gspace=1.3
 coutline=grayee
 refline=.5 cref=gray66 lref=33
 anno=anno_summary
 html=hovertext
 des='' name="&name";
run;

quit;
ODS HTML CLOSE;
ODS LISTING;
