BookmarkSubscribeRSS Feed
JaneS
Calcite | Level 5

I have a question how to create a clustered graph is SAS. 

 

The data is the following:

 

YearCountyStatewide
201520.00%50.00%
201640.00%32.00%
201730.00%12.00%
201870.00%0.00%
201990.00%80.00%
2020100.00%90.00%
2021100.00%100.00%

 

And the graph I am trying to create in SAS is the following: 

 

JaneS_0-1617991907196.png

Could you please help with the code for it? I was thinking to use proc gchart but then it looks like there should be two vbar arguments and I am not sure how to combine them.

 

Thank you!

3 REPLIES 3
ballardw
Super User

@JaneS wrote:

I have a question how to create a clustered graph is SAS. 

 

The data is the following:

 

Year County Statewide
2015 20.00% 50.00%
2016 40.00% 32.00%
2017 30.00% 12.00%
2018 70.00% 0.00%
2019 90.00% 80.00%
2020 100.00% 90.00%
2021 100.00% 100.00%

 

And the graph I am trying to create in SAS is the following: 

 

JaneS_0-1617991907196.png

Could you please help with the code for it? I was thinking to use proc gchart but then it looks like there should be two vbar arguments and I am not sure how to combine them.

 

Thank you!


To have reasonable control over the "clustering" you need to reshape the data a bit so that the same variable holds the county and statewide as values.

Here is an example:

data have;
   input Year	County:percent8.	Statewide:Percent8.;
datalines;
2015	20.00%	50.00%
2016	40.00%	32.00%
2017	30.00%	12.00%
2018	70.00%	0.00%
2019	90.00%	80.00%
2020	100.00%	90.00%
2021	100.00%	100.00%
;

proc transpose data=have out=need;
   by year;
run;   


proc sgplot data=need;
   vbar year / response=COL1 group=_name_
              groupdisplay=cluster;
   format col1 percent8.;
   label _name_='Source'
         col1 ='Value'
   ;
run;
    

Proc transpose is a basic tool for reshaping the data. I did not get into any of the options for renaming variables.

I strongly suggest use of SGPLOT instead of Gchart as there are more options to control appearance and is where SAS is concentrating improvements.

JaneS
Calcite | Level 5

Thank you so much, it was very helpful!

One more question what would be the best wat to show the percentage for each bar?

ballardw
Super User

@JaneS wrote:

Thank you so much, it was very helpful!

One more question what would be the best wat to show the percentage for each bar?


I suggest looking at the documentation here: https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.4&docsetId=grstatproc&docsetTarget=n...

Knowing where to look is half the battle and since the SGPLOT procedure has roughly 50 different statements, some with 30+ options spend some getting famililiar.

 

The option DATALABEL will show default labels of values, or if have a different variable in the data set you want to show then Datalabel=thatvariablename 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 913 views
  • 4 likes
  • 2 in conversation