BookmarkSubscribeRSS Feed
helloagainoh2
Calcite | Level 5

Hi, I am wanting to know if there is an easy way to group variables together on the x-axis. I have a column for the states, and and on each of their row I have gas prices. I would like my barchart graph to have 4 regions on the bottom, region 1,2,3,4. With the mean of gas prices for the states in those regions.

 

I am able to produce a graph that has 50 points on the x axis, is there a way I can say these states should be in region 1, and these states should be in region 2, and so on? 

3 REPLIES 3
PaigeMiller
Diamond | Level 26

A possibility

 

  1. Create a custom format assigning multiple states to regions
  2. PROC SUMMARY to create the mean by region
  3. PROC SGPLOT VBAR on the output from PROC SUMMARY
--
Paige Miller
Rick_SAS
SAS Super FREQ

You don't need to summarize the data. The VBAR statement will do that for you.

 

data Have;
length State $2;
input State Gas;
datalines;
AL      2.89
FL      2.93
CA      3.55
NV      2.99
MN      3.05
MI      3.12
NY      3.24
MA      3.33
;

proc format;
value $Region
 'AL', 'FL' /* etc */ = 'SE'
 'NY', 'MA' /* etc */ = 'NE'
 'MN', 'MI' /* etc */ = 'MW'
 'CA', 'NV' /* etc */ = 'W';
run;

proc sgplot data=Have;
format State $Region.;
vbar State / response=Gas stat=mean;
run;
JeffMeyers
Barite | Level 11

If I'm understanding correctly I think I have something close to what you're looking for.  All you would need is to add another column containing the region you want each state to be in for the grouping.

 

 

proc sgpanel data=sashelp.cars;
    panelby origin / rows=1 columns=3 novarname uniscale=row;
    vbar type / response=msrp stat=mean;
run;

 

 

I used SGPANEL so that you can use your region grouping variable in the panels while keeping the TYPE variable (which would be the states in your case) on the x-axis for categorizing the bars.  There are options I believe for removing the borders and such, only option I didn't see was a way to move the grouping labels (origin in the example) to the bottom.  The Uniscale option makes each panel able to have unique column axes.

 

SGPanel2.png

 

 

 

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 3 replies
  • 1176 views
  • 0 likes
  • 4 in conversation