I am having a hard time coming up with the graph that I posted below. The code that I have so far is posted below.
Can anyone help me out?
ods listing close;
proc means data=sashelp.cars min max mean median;
class type origin;
var mpg_city;
ods output summary=cars;
run;
proc sgplot data=cars;
highlow x=type low=mpg_city_min high=mpg_city_max / group=type type=bar transparency=.3
dataskin=pressed fillattrs=(color=cx66FF66) barwidth=.7;
footnote j=left 'Across Vehicle Types';
run;
proc means data=sashelp.cars nway min max ;
class type ;
var mpg_city MPG_Highway;
ods output summary=cars;
run;
proc sgplot data=cars;
highlow x=type low=mpg_city_min high=mpg_city_max / type=bar transparency=.5
dataskin=pressed barwidth=.7 fillattrs=graphdata1 nooutline;
highlow x=type low=MPG_Highway_min high=MPG_Highway_max / type=bar transparency=.5
dataskin=pressed barwidth=.7 fillattrs=graphdata2 nooutline;
footnote j=left 'Across Vehicle Types';
run;
What characteristic is supposed to be used to determine the color? (Assuming getting the second color is the actual question).
You appear to have something in mind but since you have type as both the x axis and group variable it is not clear.
You are getting three data series: Asia, Europe and USA. If you meant to compare only two of them, as appear in your example then you need to either exclude one or possibly create a different dichotomous variable. You might want to say what the two groups in your example are.
Your example also has different width bars for each of the two groups. Since FILLATTRS and BARWIDTH only allow a single color/ width value perhaps you are looking for a DATTRMAP solution.
As others have indicated you have too much data you're trying to plot at once - primarily multiple origins. This means you have a value for each type of vehicle when you're only showing a single bar for each metric. So you either want to not consider Origin or have a plot for each origin.
Additionally, your data structure is a touch hard to work with. Changing it around, this works fine for me and the graph is pretty close to what you've shown.
1. Simplify PROC MEANS to change output data and structure
2. Add another highlow plot for second group (city vs highway)
3. Change colors in second plot
ods listing close;
proc means data=sashelp.cars min max mean median nway noprint ;
class type origin;
var mpg_city mpg_highway;
output out=cars min= max= /autoname autolabel;
run;
proc sgplot data=cars ;
where origin='Asia';
highlow x=type low=mpg_highway_min high=mpg_highway_max / group=type type=bar transparency=.3
dataskin=pressed fillattrs=(color=cx2c7fb8) barwidth=1;
highlow x=type low=mpg_city_min high=mpg_city_max / group=type type=bar transparency=.3
dataskin=pressed fillattrs=(color=cx66FF66) barwidth=.7;
footnote j=left 'Across Vehicle Types';
run;
@chrissowden wrote:
I am having a hard time coming up with the graph that I posted below. The code that I have so far is posted below.
Can anyone help me out?
ods listing close;
proc means data=sashelp.cars min max mean median;
class type origin;
var mpg_city;
ods output summary=cars;
run;
proc sgplot data=cars;
highlow x=type low=mpg_city_min high=mpg_city_max / group=type type=bar transparency=.3
dataskin=pressed fillattrs=(color=cx66FF66) barwidth=.7;
footnote j=left 'Across Vehicle Types';
run;
proc means data=sashelp.cars nway min max ;
class type ;
var mpg_city MPG_Highway;
ods output summary=cars;
run;
proc sgplot data=cars;
highlow x=type low=mpg_city_min high=mpg_city_max / type=bar transparency=.5
dataskin=pressed barwidth=.7 fillattrs=graphdata1 nooutline;
highlow x=type low=MPG_Highway_min high=MPG_Highway_max / type=bar transparency=.5
dataskin=pressed barwidth=.7 fillattrs=graphdata2 nooutline;
footnote j=left 'Across Vehicle Types';
run;
I tried to run your code and I don't get the correct legend indicated 'city' and 'highway' with two colors.
how to correct it?
Thanks.
OK. Here is what I got.
proc means data=sashelp.cars nway min max ;
class type ;
var mpg_city MPG_Highway;
ods output summary=cars;
run;
title 'Gas range';
proc sgplot data=cars ;
highlow x=type low=MPG_Highway_min high=MPG_Highway_max / type=bar transparency=.5
dataskin=pressed barwidth=.6 fillattrs=(color=blue) nooutline legendlabel='HighWay' name='b';
highlow x=type low=mpg_city_min high=mpg_city_max / type=bar transparency=.5
dataskin=pressed barwidth=.8 fillattrs=(color=green) nooutline legendlabel='City' name='a';
keylegend 'a' 'b' / location=inside position=ne ;
yaxis min=0 label='MPG';
footnote j=left 'Across Vehicle Types';
run;
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.
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.
Ready to level-up your skills? Choose your own adventure.