Data visualization with SAS programming

combine two graphs into one graph

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

combine two graphs into one graph

I would like to combine the two graphs into one graph.  The xaxis is the same for both.

Below is my data and code.  I am using SAS 9.3.  Thank you.

data work.agentdata;

  input @1 agent_type $11.

          @13 count_one

          @17 count_two

          @21 yearmon2 $6.

          @29 ratio 7.5;

  datalines;

All Agents  122 38  Jan-14  0.31148

Agent Three 32  12  Jan-14  0.375

Agent One   83  25  Jan-14  0.3012

Agent Two   7   1    Jan-14   0.14286

All Agents  152 29  Feb-14  0.19079

Agent Three 46  8   Feb-14  0.17391

Agent One   91  20  Feb-14  0.21978

Agent Two   15  1   Feb-14  0.06667

All Agents   192 54  Mar-14  0.28125

Agent Three 48  15  Mar-14  0.3125

Agent One 130 38  Mar-14  0.29231

Agent Two 14  1   Mar-14  0.07143

All Agents 381 122 Apr-14  0.32021

Agent Three 104 45  Apr-14  0.43269

Agent One 249 73  Apr-14 0.29317

Agent Two 28  4   Apr-14 0.14286

All Agents 372 141 May-14 0.37903

Agent Three 95  45  May-14 0.47368

Agent One 257 93  May-14 0.36187

Agent Two 20  3   May-14 0.15

All Agents 339 137 Jun-14 0.40413

Agent Three 96  48  Jun-14 0.5

Agent One 223 88  Jun-14 0.39462

Agent Two 20  1   Jun-14 0.05

All Agents 288 101 Jul-14 0.35069

Agent Three 95  41  Jul-14 0.43158

Agent One 167 56  Jul-14 0.33533

Agent Two 26  4   Jul-14 0.15385

All Agents 206 70  Aug-14 0.33981

Agent Three 72  29  Aug-14 0.40278

Agent One 113 39  Aug-14 0.34513

Agent Two 21  2   Aug-14 0.09524

All Agents 154 65  Sep-14 0.42208

Agent Three 47  22  Sep-14 0.46809

Agent One 98  41  Sep-14 0.41837

Agent Two 9   2   Sep-14 0.22222

All Agents 139 36  Oct-14 0.25899

Agent Three 35  9   Oct-14 0.25714

Agent One 93  26  Oct-14 0.27957

Agent Two 11  1   Oct-14 0.09091

All Agents 86  29  Nov-14 0.33721

Agent Three 30  14  Nov-14 0.46667

Agent One 44  12  Nov-14 0.27273

Agent Two 12  3   Nov-14 0.25

All Agents 115 29  Dec-14 0.25217

Agent Three 40  14  Dec-14 0.35

Agent One 62  13  Dec-14 0.20968

Agent Two 13  2   Dec-14 0.15385

;

  /***************************************************************************************************************/

  /* CREATE A STACKED BAR CHART FOR COUNT_TWO GROUP BY AGENT BY YEARMON */

  /* EXCLUDE AGENT TYPE OF 'ALL AGENTS'                                                                       */

  /**************************************************************************************************************/

proc sgplot data=work.agentdata (where = (agent_type ne 'All Agents'));

     vbar yearmon2 / response = count_two

                            group = agent_type                  

                            name='bar'

                            barwidth = 0.5;

     xaxis discreteorder=data;

     yaxis grid

              label="Count";

     keylegend "bar";

run;

  /*****************************************************************************************/

  /* CREATE A SINGLE LINE FOR RATIO FOR ALL AGENTS BY YEARMON */

  /* FOR AGENT TYPE OF  "ALL AGENTS" ONLY                                        */

  /*****************************************************************************************/

proc sgplot data=work.agentdata (where = (agent_type eq 'All Agents'));

     vline yearmon2 / response = ratio                    

                             y2axis

                             markers

                             markerattrs=(symbol=circlefilled color=mor)

                             lineattrs=(color=mor)

                             name='line'

                             legenlabel = "ratio";

     xaxis discreteorder=data;

     y2axis label="Ratio"

               offsetmin=0

               offsetmax=0

               thresholdmax = 1

               thresholdmin = 1;

               keylegend "line";

run;


Accepted Solutions
Solution
‎05-12-2015 12:00 PM
Super Contributor
Posts: 252

Re: combine two graphs into one graph

Try this code below, although you will have to make sure that the ratio corresponds with the counts. Also you will need to add the markers and the appropriate legends.

proc template;
  define statgraph stacked;
    begingraph;
   layout overlay / xaxisopts = (label = "Year and Month")
                       yaxisopts = (label = "Count")
                       y2axisopts = (label = "Ratio");
     barchart x = eval(ifc(agent_type ne 'All Agents', yearmon2, "")) y = count_two / group = agent_type barwidth = 0.5;
     seriesplot x = eval(ifc(agent_type =  'All Agents', yearmon2, "")) y = ratio / yaxis = y2 markerattrs=(symbol=circlefilled color=mor) lineattrs=(color=mor);
   endlayout;
    endgraph;
  end;
run;

proc sgrender data = agentdata template=stacked;
run;

View solution in original post


All Replies
Super User
Posts: 11,101

Re: combine two graphs into one graph

Solution
‎05-12-2015 12:00 PM
Super Contributor
Posts: 252

Re: combine two graphs into one graph

Try this code below, although you will have to make sure that the ratio corresponds with the counts. Also you will need to add the markers and the appropriate legends.

proc template;
  define statgraph stacked;
    begingraph;
   layout overlay / xaxisopts = (label = "Year and Month")
                       yaxisopts = (label = "Count")
                       y2axisopts = (label = "Ratio");
     barchart x = eval(ifc(agent_type ne 'All Agents', yearmon2, "")) y = count_two / group = agent_type barwidth = 0.5;
     seriesplot x = eval(ifc(agent_type =  'All Agents', yearmon2, "")) y = ratio / yaxis = y2 markerattrs=(symbol=circlefilled color=mor) lineattrs=(color=mor);
   endlayout;
    endgraph;
  end;
run;

proc sgrender data = agentdata template=stacked;
run;

Occasional Contributor
Posts: 5

Re: combine two graphs into one graph

Thank you.  Your answer was very helpful.  I have the following code.  The only thing that I have been unable to do is to add markers to the seriesplot.  Is there a way to do that?

proc template;

  define style styles.bis;

  parent = styles.htmlblue;

  class GRAPHDATA2 / contrastcolor=mor

                           color=mor;   

  class GRAPHDATA3 / contrastcolor=vigb

                           color=vigb;

  class GRAPHDATA4 / contrastcolor=vilg

                           color=vilg;

  end;

run;

proc template;

  define statgraph stacked;

  begingraph;

  entrytitle textattrs=(size=9pt) 'Title One' ;

  entrytitle textattrs=(size=8pt weight=normal) 'Title Two';

  layout overlay / xaxisopts = (display=(ticks tickvalues) tickvalueattrs=(size=7))

                             yaxisopts = (label = "Count" offsetmin=0 offsetmax=0 griddisplay=on 

                                          linearopts=(thresholdmin=1 thresholdmax=1)

   tickvalueattrs=(size=7))

                             y2axisopts = (label = "Ratio" offsetmin=0 offsetmax=0

                                           linearopts=(thresholdmin=1 thresholdmax=1)

                                           tickvalueattrs=(size=7));

  barchart   x = eval(ifc(agent_type ne 'All Agents', yearmon2, ""))

                       y = count_two / group = agent_type

                                       barwidth = 0.5

                                    name='bar';

  seriesplot x = eval(ifc(agent_type = 'All Agents', yearmon2, ""))

                       y = ratio / yaxis = y2

                                lineattrs=(color=mor thickness=2)

                                   name='series';                                                                             

  discretelegend 'bar' 'series' / exclude=('All Agents')

                                            border=false

                                            valueattrs=(size= 7pt);

  endlayout;

       entryfootnote halign=left textattrs=(size=8pt style=normal) 'Entry Footnote';

  endgraph;

  end;

run;

ods listing style=bis;

ods html style=bis;

proc sgrender data = agentdata template=stacked; 

  format ratio percent7.2;

run;

title;

SAS Super FREQ
Posts: 925

Re: combine two graphs into one graph

Use DISPLAY=ALL on the seriesplot to get the markers.

Occasional Contributor
Posts: 5

Re: combine two graphs into one graph

Thank you.  Display=All worked.

Super Contributor
Posts: 252

Re: combine two graphs into one graph

Thank you Dan. I was wondering why the markers weren't showing up on the seriesplot. If I didn't know your solution, I would have suggested using the Scatterplot statement to add the markers.

🔒 This topic is solved and locked.

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

Discussion stats
  • 6 replies
  • 533 views
  • 6 likes
  • 4 in conversation