BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Amber1331
Calcite | Level 5

Using SAS 9.4 I am trying to create a bar chart (with various other points on it) with a table on the bottom. Currently my bar chart contains the observed and I am using a marker to show the expected. I would like to include a table at the bottom that has the delta between the observed and the expected. From what I read I need to use proc sgrender because my bar chat contains bars and a scatterplot, however that is not causing me a problem when writing annotate ( I will admit this is my first time using annotate). Without the annotate, my bar chart works percectly and I have my desired chart, without the table. Unfortunately, when I add the annotate code, nothing happens to my chart. I'm looking for some help as to whether I am missing something with my code or whether there is an easier way to achieve my desired graph. 

 

ods graphics/ width=8in;
proc template;
define statgraph percent_target;
begingraph;
layout overlay / xaxisopts=(label='Grade')
yaxisopts=(label='Percent' display=none)
borderattrs=(color=white)
walldisplay=none;
barchart x=grade_level y=percent_Target /orient=vertical barlabel=True barlabelattrs=(size=12pt) fillattrs=(color=CX4F81BD)barwidth=.5 outlineattrs=(color=CX4F81BD)
name='percent' legendlabel='Observed YTD Growth, % of Target';
annotate;
scatterplot x=grade_level y=MOY_Expected_PTarget / markerattrs=(color=CXED7D31 symbol=squarefilled)
name='expected' legendlabel='Expected YTD Growth, % of Target';
referenceline y=1 /curvelabel='Annual Target 100%' lineattrs=(color=CX00B050)curvelabelattrs=(size=12pt);
/*seriesplot x=grade_level y=annual_target / lineattrs=(color=CX00B050) */
/* name='annual' legendlabel='Annual Growth Target';*/
discretelegend 'percent' 'expected'/ border=false;
endlayout;
endgraph;
end;
run;

/*Insert the Annotate code*/

data delta; set delta;
if grade_level='K' then grade=00;
else grade=grade_level;
run;

data delta; set delta;
drop grade_level;
run;

data matha readinga; set delta;
if subject='Mathematics' then output matha;
if subject='Reading' then output readinga;
run;

data matha; set matha;
drop subject;
run;

proc sort data=matha;
by grade;
run;

data annotate1; set matha; by grade;
x=grade; y=0.3; format text $50. function ;
function='label';
text=(round((delta*100),1));
position='8';
size=1;
xysy='2'; ysys='2';
output;
run;

 

title1 j=left height=15 bold "Growth";
title2 j=left height=8 bold "Percentage of target achieved, compared to percent of target expectations.";
footnote1 j=left height=3.5 "This is the percent of target achieved at this point of time.";
proc sgrender data=g_p_target template=percent_target sganno=annotate1;
where subject='Mathematics';
run;

1 ACCEPTED SOLUTION

Accepted Solutions
Jay54
Meteorite | Level 14

Here is sample code:

 

data bar;
  input Product $ Actual Expected;
  Diff=Expected-Actual;
  datalines;
Chairs 300 200
Tables 250 300
Lamps 200 150
Sofas 100 120
;
  run;

 

ods html close;
ods listing gpath='C:\Work\SASUser';
ods graphics / reset width=5in height=3in imagename='BarDiff';
proc sgplot data=bar;
  vbarparm category=product response=actual;
  scatter x=product y=expected / markerattrs=(symbol=circlefilled size=10);
  xaxistable diff / location=inside position=top valueattrs=(size=10) separator;
  yaxis offsetmin=0;
run;

 


BarDiff.png

View solution in original post

6 REPLIES 6
Jay54
Meteorite | Level 14

You may be making this more complex than necessary.  You can likely use SGPLOT for this.  If data is summarized by category (or you can summarize it using Proc Means), you can use a VBARPARM to draw the bar, and overlay a SCATTER or HIGHLOW (with same high and low values).  Compute the differences in another column and use a XAXISTABLE to draw the difference values. 

Jay54
Meteorite | Level 14

Here is sample code:

 

data bar;
  input Product $ Actual Expected;
  Diff=Expected-Actual;
  datalines;
Chairs 300 200
Tables 250 300
Lamps 200 150
Sofas 100 120
;
  run;

 

ods html close;
ods listing gpath='C:\Work\SASUser';
ods graphics / reset width=5in height=3in imagename='BarDiff';
proc sgplot data=bar;
  vbarparm category=product response=actual;
  scatter x=product y=expected / markerattrs=(symbol=circlefilled size=10);
  xaxistable diff / location=inside position=top valueattrs=(size=10) separator;
  yaxis offsetmin=0;
run;

 


BarDiff.png
Jay54
Meteorite | Level 14

Alternate appearance.  Except including the "Expected" in the legend.  Could be done with a few more gyrations.

 

proc sgplot data=bar;
  vbarparm category=product response=actual / name='a';
  highlow x=product high=expected low=expected / type=bar barwidth=0.4
               discreteoffset=-0.1 legendlabel='Expected';
  xaxistable diff / location=inside position=top valueattrs=(size=10) separator;
  yaxis offsetmin=0 offsetmax=0.1 display=(nolabel);
  xaxis display=(nolabel);
  keylegend 'a';
run;


BarDiff2.png
Amber1331
Calcite | Level 5

Thank you for all your help. This created the exact graph and table combination that I needed. 

Rick_SAS
SAS Super FREQ

Here's an example of putting the expected value on a bar chart: (scroll about halfway down the article):
"Fitting a Poisson distribution to data in SAS"

It uses the VBARPARM and SCATTER statements.  If you have SAS 9.4, you can use the XAXISTABLE statement to draw the text. Otherwise, use another SCATTER statement with the MARKERCHAR= option to get the text onthe graph.

Amber1331
Calcite | Level 5

Thank you for the article. This will be helpful. 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1862 views
  • 0 likes
  • 3 in conversation