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

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;

example1.PNG

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
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;

View solution in original post

5 REPLIES 5
ballardw
Super User

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.

Reeza
Super User

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;

example1.PNG

 


 

Ksharp
Super User
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;
e75wez1
Calcite | Level 5

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.

 

mileage.png

Ksharp
Super User

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;

x.png

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 5 replies
  • 1875 views
  • 3 likes
  • 5 in conversation