0

I'm using sas sgplot to plot on variable with 3 responses. I want it to plot data from 3 columns. 1st column is total counts for an entity, 2nd column is violation counts for the same entity, and the 3rd column is percent violations. I'm using proc sgplot. I give it three vbar statements one for each response. So the bars are overplayed , but the percent response places the percent values right in the middle of the bars so it looks sloppy. I would like the percent values to appear right above the bar, but not in a straight line across but right above it, since the bars are different heights. I know I need to create an anno set, but haven't done that before. Can someone please help. Here is my code for the chart I have now. Thanks

proc sgplot data=k noborder nowall ;
vbar Entity_Name / response=tot_trans_count  dataskin=GLOSS      LEGENDLABEL="Number of Transactions" 
DATALABELPOS=TOP DATALABEL DATALABELATTRS=(Color=blue Family=Arial Size=10 Style=Italic Weight=BOLD ) 
barwidth=0.8
transparency=0.0;
YAXIS DISPLAY=NONE; 

vbar Entity_Name /  response=violation_count  dataskin=GLOSS      LEGENDLABEL="Number of Violations" 
DATALABELPOS=BOTTOM DATALABEL  DATALABELATTRS=(Color=red Family=Arial Size=10 Style=Italic Weight=BOLD) 
    barwidth=0.8
    transparency=0.0;
    YAXIS DISPLAY=NONE; 

vbar Entity_Name /  response=violation_percent  dataskin=GLOSS      LEGENDLABEL="Number of Violations" 
DATALABEL  DATALABELATTRS=(Color=red Family=Arial Size=10 Style=Italic Weight=BOLD) 
    barwidth=0.8
    transparency=0.0;
    YAXIS DISPLAY=NONE; 
run;
user601828
  • 499
  • 3
  • 7
  • 17
  • Please could you provide a sample dataset (doesn't have to contain real data) and a sketch of the chart you are trying to create? – user667489 Oct 01 '15 at 15:15
  • Trader Name % Violations of Total Transactions Total Violations Total Transactions Name1 20% 10 2 Name2 56% 171 96 Name3 30% 43 13 Name4 9% 23 2 Name5 24% 111 27 Name6 58% 429 248 Name7 39% 180 71 Name8 33% 6 2 Name9 41% 70 29 Name10 50% 10 5 Name11 3% 31 1 Name12 67% 9 6 Name13 23% 125 29 Name14 10% 126 12 Name15 32% 237 75 Name16 15% 27 4 Name17 25% 28 7 Name18 100% 3 3 Name19 30% 138 42 Name20 22% 763 165 Name21 100% 2 2 Name22 22% 106 23 Name23 20% 103 21 Name24 5% 20 1 Name25 3% 1,463 37 1st column name, 2nd total transactions, 3rd total violations, 4 % violations of total. – user601828 Oct 01 '15 at 15:17
  • Please add it at the bottom of your question - it will be much easier to read that way. – user667489 Oct 01 '15 at 15:18

1 Answers1

1

I have a non-annotate solution below. This displays text for the percent at the top of the highest bar and includes the percent in the legend. But does not show a bar for percent as I think it's incorrect to show percent and counts on the same scale when they are different concepts. See if this is what you want.

data k;
    infile cards;
    input Entity_Name $ violation_percent_char $ tot_trans_count violation_count ;
    *** CONVERT PERCENT VARIABLE FROM CHARACTER TO NUMERIC ***;
    violation_percent = input( compress(violation_percent_char, '%'), 3.0);
    cards;
Name1 20% 10 2 
Name2 56% 171 96 
Name3 30% 43 13 
Name4 9% 23 2 
Name5 24% 111 27 
Name6 58% 429 248 
Name7 39% 180 71 
Name8 33% 6 2 
Name9 41% 70 29 
Name10 50% 10 5 
Name11 3% 31 1 
Name12 67% 9 6 
Name13 23% 125 29 
Name14 10% 126 12 
Name15 32% 237 75 
Name16 15% 27 4 
Name17 25% 28 7 
Name18 100% 3 3 
Name19 30% 138 42 
Name20 22% 763 165 
Name21 100% 2 2 
Name22 22% 106 23 
Name23 20% 103 21 
Name24 5% 20 1 
Name25 3% 1463 37 
;
run;


proc sgplot data=k ;*noborder nowall ;

*** PLOT TOTAL COUNT AND USE THIS TO REPORT THE PERCENT BY USING THE DATALABEL OPTION ***;
*** BY SPECIFYING THIS VBAR STATEMENT FIRST, THIS BAR WILL BE OVERLAID BY THE NEXT VBAR FOR "TOTAL" ***;
*** ATTEMPT TO COORDINATE THE TEXT COLOR WITH THE LEGEND COLOR IN THE GRAPH ***;  
vbar Entity_Name /  response=tot_trans_count  dataskin=GLOSS      LEGENDLABEL="Percent of Violations" 
DATALABEL  DATALABEL=violation_percent_char DATALABELATTRS=(Color=BLUE Family=Arial Size=10 Style=Italic Weight=BOLD) 
barwidth=0.8
transparency=0.0;

vbar Entity_Name / response=tot_trans_count  dataskin=GLOSS      LEGENDLABEL="Number of Transactions" 
DATALABELPOS=TOP DATALABEL DATALABELATTRS=(Color=red Family=Arial Size=10 Style=Italic Weight=BOLD ) 
barwidth=0.8
transparency=0.0;
*YAXIS DISPLAY=NONE; 

vbar Entity_Name /  response=violation_count  dataskin=GLOSS      LEGENDLABEL="Number of Violations" 
DATALABELPOS=BOTTOM DATALABEL  DATALABELATTRS=(Color=green Family=Arial Size=10 Style=Italic Weight=BOLD) 
barwidth=0.8
transparency=0.0;
*YAXIS DISPLAY=NONE; 


*** ONLY NEED 0NE YAXIS STATEMENT FOR ENTIRE SGPLOT ***;
YAXIS DISPLAY=NONE; 
run;
SunnyRJ
  • 383
  • 1
  • 7
  • Hi Sunny, Thank you so much. You are a genious...that is incredibly clever and creative. Wow...and Bravo. I really needed this help..I even called SAS help and they tried but couldnt do it or offer any workaround. This code is truly inspiring. Thank you very much. All the best to you. – user601828 Oct 02 '15 at 23:48
  • 1
    Glad to hear this worked for you! Years ago I did lots of SAS/GRAPH work and had to come up with clever tricks for some of my plots. The new SG plots have nicer output, but sometimes still need good tricks for output. If you could "accept" the above answer, that would be great. Cheers. – SunnyRJ Oct 06 '15 at 20:32
  • Ok I found it...thank you so much again. Your answer is inspirational. – user601828 Oct 07 '15 at 16:06