%let gpath='C:\Work\Blogs\Graphically Speaking\2016\Post_31_Aug_15_A1c'; ods html close; %let dpi=200; ods listing gpath=&gpath image_dpi=&dpi; /*--Data for A1c and events--*/ data A1C; informat Date date7.; format Date date7.; input Date A1C Event; datalines; 01Jan16 7.1 . 07Jan16 . 6.8 15Jan16 . . 16Jan16 . . 20Jan16 . . 22Jan16 8.0 . 23Jan16 . 6.8 24Jan16 . . 27Jan16 . . 29Jan16 . . 31Jan16 7.2 . ; run; /*ods html;*/ /*proc print;run;*/ /*ods html close;*/ /*--Data for Meds--*/ data Meds; informat From To date7.; format From To date7.; input Meds $ From To Group Label $40-44; if group=1 then Dosage='10mg'; else Dosage='20mg'; datalines; Med-1 '07Jan16'd '15Jan16'd 1 Med-1 Med-1 '20Jan16'd '23Jan16'd 2 Med-2 '23Jan16'd '29Jan16'd 1 Med-2 Med-3 '15Jan16'd '25Jan16'd 1 Med-3 ; run; /*ods html;*/ /*proc print;*/ /*var Meds Label Group Dosage From To;*/ /*run;*/ /*ods html close;*/ /*--Merge data--*/ data both; merge a1c meds; run; /**/ /*ods html;*/ /*proc print;*/ /*var Date A1c Event Meds Label Group Dosage From To;*/ /*run;*/ /*ods html close;*/ /*--Attribute maps for Line groups--*/ data attrmap; id='Meds'; value='1'; linestyle='GraphData1'; linethickness=1; output; id='Meds'; value='2'; linestyle='GraphData1'; linethickness=3; output; run; /*--Graph using GTL--*/ proc template; define statgraph A1c_1; begingraph; entrytitle 'A1c by Date with Events and Meds'; layout lattice / rows=2 columndatarange=union rowweights=(0.7 0.3) rowgutter=5px; columnaxes; columnaxis / griddisplay=on display=(ticks tickvalues); endcolumnaxes; layout overlay / yaxisopts=(griddisplay=on tickvalueattrs=(size=8) linearopts=(viewmin=6 tickvaluesequence=(start=6 end=8 increment=0.2))); seriesplot x=date y=a1c / lineattrs=(thickness=2); scatterplot x=date y=event / markerattrs=(symbol=trianglefilled size=12); endlayout; layout overlay / yaxisopts=(display=(label) reverse=true); highlowplot y=meds low=from high=to / group=group lowlabel=label highlabel=dosage lineattrs=(color=black) labelattrs=(color=black); endlayout; endlayout; endgraph; end; run; ods graphics / reset attrpriority=color width=5in height=3in imagename='A1C_1_GTL'; proc sgrender data=both template=a1c_1 dattrmap=attrmap; dattrvar group='Meds'; run; /*--Graph 2 using GTL--*/ proc template; define statgraph A1c_2; begingraph; entrytitle 'A1c by Date with Events and Meds'; layout lattice / rows=2 columndatarange=union rowweights=(0.7 0.3) rowgutter=5px; columnaxes; columnaxis / griddisplay=on display=(ticks tickvalues); endcolumnaxes; layout overlay / yaxisopts=(griddisplay=on tickvalueattrs=(size=8) linearopts=(viewmin=6 tickvaluesequence=(start=6 end=8 increment=0.2))); seriesplot x=date y=a1c / lineattrs=(thickness=2); scatterplot x=date y=event / markerattrs=(symbol=trianglefilled size=12); endlayout; layout overlay / yaxisopts=(display=(label) reverse=true); highlowplot y=meds low=from high=to / group=group lowlabel=label lineattrs=(color=black) labelattrs=(color=black); highlowplot y=meds low=from high=to / group=group highlabel=dosage lineattrs=(thickness=0) labelattrs=(color=gray size=5); endlayout; endlayout; endgraph; end; run; ods graphics / reset attrpriority=color width=5in height=3in imagename='A1C_2_GTL'; proc sgrender data=both template=A1c_2 dattrmap=attrmap; dattrvar group='Meds'; run; /*--Graph 3 using GTL with Legends--*/ proc template; define statgraph A1c_3; begingraph; entrytitle 'A1c by Date with Events and Meds'; layout lattice / rows=2 columndatarange=union rowweights=(0.7 0.3) rowgutter=5px; columnaxes; columnaxis / griddisplay=on display=(ticks tickvalues); endcolumnaxes; layout overlay / yaxisopts=(griddisplay=on tickvalueattrs=(size=8) linearopts=(viewmin=6 tickvaluesequence=(start=6 end=8 increment=0.2))); seriesplot x=date y=a1c / lineattrs=(thickness=2) name='a'; scatterplot x=date y=event / markerattrs=(symbol=trianglefilled size=10) name='b'; discretelegend 'a' 'b' / location=inside halign=left valign=top across=2 border=on opaque=true itemsize=(linelength=20) autoitemsize=true; endlayout; layout overlay / yaxisopts=(display=(label) reverse=true); highlowplot y=meds low=from high=to / group=group lowlabel=label; highlowplot y=meds low=from high=to / group=group highlabel=dosage lineattrs=(thickness=0) labelattrs=(size=6); endlayout; endlayout; endgraph; end; run; ods graphics / reset attrpriority=color width=5in height=3in imagename='A1C_3_GTL'; proc sgrender data=both template=A1c_3 dattrmap=attrmap; dattrvar group='Meds'; run; ods listing image_dpi=100; ods graphics / reset attrpriority=color width=2.7in height=1.8in imagename='A1C_Icon'; proc sgrender data=both template=A1c_2 dattrmap=attrmap; dattrvar group='Meds'; run; /*--Graph using SG--*/ ods listing image_dpi=200; ods graphics / reset attrpriority=color width=5in height=3in imagename='A1C_SG'; title 'A1c by Date with Events and Meds'; proc sgplot data=both noautolegend dattrmap=attrmap; series x=date y=a1c / lineattrs=(thickness=2) name='a'; scatter x=date y=event / markerattrs=(symbol=trianglefilled size=12) name='b'; keylegend 'a' 'b' / location=inside position=topleft border opaque linelength=20 autoitemsize; refline 5.8 / noclip; highlow y=meds low=from high=to / y2axis group=group attrid=Meds lowlabel=label; highlow y=meds low=from high=to / y2axis group=group attrid=Meds highlabel=dosage labelattrs=(size=6) lineattrs=(thickness=0); yaxis offsetmin=0.3 offsetmax=0.05 values=(6 to 8 by 0.2) min=5.8 max=8 valueshint label='A1c' labelpos=datacenter valueattrs=(size=8) grid; y2axis offsetmin=0.75 offsetmax=0.07 display=none reverse; xaxis display=(nolabel) grid; run; title;