Data visualization with SAS programming

Proc Sgrender and Annotate

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 15
Accepted Solution

Proc Sgrender and Annotate

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;


Accepted Solutions
Solution
‎10-22-2015 04:57 PM
SAS Super FREQ
Posts: 1,081

Re: Proc Sgrender and Annotate

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;

 

View solution in original post

Attachment

All Replies
SAS Super FREQ
Posts: 1,081

Re: Proc Sgrender and Annotate

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. 

Solution
‎10-22-2015 04:57 PM
SAS Super FREQ
Posts: 1,081

Re: Proc Sgrender and Annotate

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;

 

Attachment
SAS Super FREQ
Posts: 1,081

Re: Proc Sgrender and Annotate

[ Edited ]

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;

Attachment
Occasional Contributor
Posts: 15

Re: Proc Sgrender and Annotate

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

SAS Super FREQ
Posts: 3,416

Re: Proc Sgrender and Annotate

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.

Occasional Contributor
Posts: 15

Re: Proc Sgrender and Annotate

Thank you for the article. This will be helpful. 

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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