Help using Base SAS procedures

GTL: Numbers Inside Horizontal Bars

Reply
N/A
Posts: 0

GTL: Numbers Inside Horizontal Bars

Hi,

I'd like to produce a horizontal bar chart with percents displayed inside the bar segments. Can I do it with GTL? My code is below.

Thanks,
Randy



data testData;
length var $ 3;

var='A11'; val=68; seg='Positive'; output;
var='A11'; val=14; seg='Neutral'; output;
var='A11'; val=18; seg='Negative'; output;

var='A4'; val=77; seg='Positive'; output;
var='A4'; val=11; seg='Neutral'; output;
var='A4'; val=12; seg='Negative'; output;

var='A3'; val=85; seg='Positive'; output;
var='A3'; val=7; seg='Neutral'; output;
var='A3'; val=8; seg='Negative'; output;

var='A1'; val=85; seg='Positive'; output;
var='A1'; val=6; seg='Neutral'; output;
var='A1'; val=9; seg='Negative'; output;
run;

ods graphics / reset;
ods listing close;
ods html file='test.html' path='c:\temp' style=Science;
proc template;
define statgraph barchartparm;
begingraph / border=false;
layout overlay / xaxisopts=(display=none) yaxisopts=(display=none) WALLDISPLAY=NONE;
barchartparm x=var y=val / group=seg orient=horizontal;
*entry "Test line";
endlayout;
endgraph;
end;
run;

proc sgrender data=testData template=barchartparm;
run;
ods listing;
ods html close;
N/A
Posts: 0

Re: GTL: Numbers Inside Horizontal Bars

Posted in reply to deleted_user
Randy,
I asked around and found some really great information based on your post. I'll try to work it into something more complete later. Here's what I found out.

GTL grouped (Stacked) bar chart does not display segment labels. However, you can achieve this by overlaying a scatter plot to display labels. You could also add a legend to see the color to value mapping.

Try to following code to accomplish this.

[pre]
data testData (drop=prev);
length var $ 3;
retain prev 0;
var='A11'; val=68; seg='Positive'; seg2='+ive'; labelval= prev+val/2; prev=prev+val; output;
var='A11'; val=14; seg='Neutral'; seg2='0'; labelval= prev+val/2; prev=prev+val; output;
var='A11'; val=18; seg='Negative'; seg2='-ive'; labelval= prev+val/2; prev=0; output;

var='A4'; val=77; seg='Positive'; seg2='+ive'; labelval= prev+val/2; prev=prev+val; output;
var='A4'; val=11; seg='Neutral'; seg2='0'; labelval= prev+val/2; prev=prev+val; output;
var='A4'; val=12; seg='Negative'; seg2='-ive'; labelval= prev+val/2; prev=0; output;

var='A3'; val=85; seg='Positive'; seg2='+ive'; labelval= prev+val/2; prev=prev+val; output;
var='A3'; val=7; seg='Neutral'; seg2='0'; labelval= prev+val/2; prev=prev+val; output;
var='A3'; val=8; seg='Negative'; seg2='-ive'; labelval= prev+val/2; prev=0; output;

var='A1'; val=85; seg='Positive'; seg2='+ive'; labelval= prev+val/2; prev=prev+val; output;
var='A1'; val=6; seg='Neutral'; seg2='0'; labelval= prev+val/2; prev=prev+val; output;
var='A1'; val=9; seg='Negative'; seg2='-ive'; labelval= prev+val/2; prev=0; output;
run;
/*proc print; run;*/

ods graphics / reset;
ods listing close;
ods html file='test.html' style=Science;
proc template;
define statgraph barchartparm1;
begingraph / border=false;
EntryTitle 'Use Scatter Plot for segment labels - Some labels collide';
layout overlay / xaxisopts=(display=none offsetmin=0 offsetmax=0) yaxisopts=(display=none) WALLDISPLAY=NONE;
barchartparm x=var y=val / group=seg orient=horizontal;
scatterplot x=labelval y=var / markercharacter=seg markercharacterttrs=(color=white weight=bold);
*entry "Test line";
endlayout;
endgraph;
end;
run;

proc sgrender data=testData template=barchartparm1;
run;

proc template;
define statgraph barchartparm2;
begingraph / border=false;
EntryTitle 'Use DiscreteLegend for segment identification';
layout overlay / xaxisopts=(display=none) yaxisopts=(display=none) WALLDISPLAY=NONE;
barchartparm x=var y=val / group=seg orient=horizontal name='bar';
discretelegend 'bar';
*entry "Test line";
endlayout;
endgraph;
end;
run;

proc sgrender data=testData template=barchartparm2;
run;


proc template;
define statgraph barchartparm3;
begingraph / border=false;
EntryTitle 'Use shorter segment labels';
layout overlay / xaxisopts=(display=none offsetmin=0 offsetmax=0) yaxisopts=(display=none) WALLDISPLAY=NONE;
barchartparm x=var y=val / group=seg2 orient=horizontal;
scatterplot x=labelval y=var / markercharacter=seg2 markercharacterattrs=(color=white size=9pt weight=bold);
*entry "Test line";
endlayout;
endgraph;
end;
run;

proc sgrender data=testData template=barchartparm3;
run;
proc template;
define statgraph barchartparm4;
begingraph / border=false;
EntryTitle 'Use Vertical Bar Chart';
layout overlay / xaxisopts=(display=none) yaxisopts=(display=none) WALLDISPLAY=NONE;
barchartparm x=var y=val / group=seg ;
scatterplot x=var y=labelval / markercharacter=seg markercharacterattrs=(color=white size=9pt weight=bold);
*entry "Test line";
endlayout;
endgraph;
end;
run;

proc sgrender data=testData template=barchartparm4;
run;
ods listing;
ods html close;
[/pre]
Ask a Question
Discussion stats
  • 1 reply
  • 123 views
  • 0 likes
  • 1 in conversation