Data visualization with SAS programming

2 datasets/series in one graph

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

2 datasets/series in one graph

Hi All

 

I am trying to make the attached graph in SAS - that is I want to make a proc gchart vbar on one dataset and a proc sqplot on another dataset  and then combine the two graphs using proc greplay, but it just doesnt look pretty - does anybody have any other ideas ?


Accepted Solutions
Solution
‎03-09-2016 09:01 AM
SAS Super FREQ
Posts: 708

Re: 2 datasets/series in one graph

Hi

 

You combine the data that needs to be plotted into one data set using different varibale names, see below for an example

Capture.PNG

 

You then can use the appropriate plot statements of Proc SGPLOT that are compatible with each other, for your example they are VBARPARM and SCATTER

 

See also this blog entry http://blogs.sas.com/content/graphicallyspeaking/2013/03/09/parametric-bar-charts/ by @Sanjay_SAS

 

Here is a code sample that creates two tables with the data to be plotted, they are combined and then used with Proc SGPLOT

 

proc sql;
  create table carsAggr1 as
  select
    origin
    , type
    , mean(horsePower) as avg_horsePower
  from
    sashelp.cars

  group by
    origin
    , type
  ;
quit;

proc sql;
  create table carsAggr2 as
  select
    origin as origin2    
    , count(*) / (select count(*) from sashelp.cars) as share_pct format=percent9.1
  from
    sashelp.cars

  group by
    origin
  ;
quit;

data carsAggr3;
  set carsAggr1 carsAggr2;
  *
  * fill it, so we do not get an additional legend entry
  *;
  if type = " " then do;
    type = "SUV";
  end;
run;

proc sgplot data=carsAggr3;
  vbarparm category=Origin response=avg_horsePower / group=Type name="bar";
  scatter x=origin2 y=share_pct / 
    y2axis 
    markerattrs=(symbol= squarefilled)
    name="scatter"
    datalabel
    datalabelpos=top
  ;
  keylegend "bar" "scatter" / 
    location=outside
    position=right
    noborder
  ;
  y2axis values=(0 to 1.2 by 0.2);
run;

Bruno

View solution in original post


All Replies
Solution
‎03-09-2016 09:01 AM
SAS Super FREQ
Posts: 708

Re: 2 datasets/series in one graph

Hi

 

You combine the data that needs to be plotted into one data set using different varibale names, see below for an example

Capture.PNG

 

You then can use the appropriate plot statements of Proc SGPLOT that are compatible with each other, for your example they are VBARPARM and SCATTER

 

See also this blog entry http://blogs.sas.com/content/graphicallyspeaking/2013/03/09/parametric-bar-charts/ by @Sanjay_SAS

 

Here is a code sample that creates two tables with the data to be plotted, they are combined and then used with Proc SGPLOT

 

proc sql;
  create table carsAggr1 as
  select
    origin
    , type
    , mean(horsePower) as avg_horsePower
  from
    sashelp.cars

  group by
    origin
    , type
  ;
quit;

proc sql;
  create table carsAggr2 as
  select
    origin as origin2    
    , count(*) / (select count(*) from sashelp.cars) as share_pct format=percent9.1
  from
    sashelp.cars

  group by
    origin
  ;
quit;

data carsAggr3;
  set carsAggr1 carsAggr2;
  *
  * fill it, so we do not get an additional legend entry
  *;
  if type = " " then do;
    type = "SUV";
  end;
run;

proc sgplot data=carsAggr3;
  vbarparm category=Origin response=avg_horsePower / group=Type name="bar";
  scatter x=origin2 y=share_pct / 
    y2axis 
    markerattrs=(symbol= squarefilled)
    name="scatter"
    datalabel
    datalabelpos=top
  ;
  keylegend "bar" "scatter" / 
    location=outside
    position=right
    noborder
  ;
  y2axis values=(0 to 1.2 by 0.2);
run;

Bruno

Occasional Contributor
Posts: 5

Re: 2 datasets/series in one graph

Posted in reply to Bruno_SAS

Hi Bruno

 

Thank you very much for the help -  it worked :-)

 

Anette

☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 721 views
  • 0 likes
  • 2 in conversation