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

Hi,

I am creating a bar chart but would like to get percentage rather than frequency.

And percentage i am interested in is not percentage of total frequency but percentage of group frequency.

My SAS code is like below:

 

ods graphics on;
proc sgplot data=red.survey2;
vbar I_feel_well_informed_about_the_C /group=Program__select_one_ groupdisplay=cluster ;
format I_feel_well_informed_about_the_C $category. ;
run;

 

And results are attached. I am interested in percentage rather than frequency in y axis.  I a way for the group agree it sums to 100% , for disagree it sums to 100%.

 

Thanks,

 

 

 

 
 

 

 
 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

I suspect your PROC FREQ call is incorrect. You want to get the row totals. The output should look like the following:

 

data Have;
length Informed Program $8; 
input Informed Program Percent;
datalines;
Agree    Anesth  41 
Agree    Emerg   26
Agree    GenSurg 33
Disagree Anesth  39 
Disagree Emerg    8
Disagree GenSurg 53
Neutral  Anesth  34 
Neutral  Emerg   11
Neutral  GenSurg 55
;

proc sgplot data=Have;
vbar Informed / response=Percent group=Program groupdisplay=cluster;
xaxis discreteorder=data;
yaxis grid values=(0 to 100 by 10) label="Percentage of Total with Group";
run;

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26
vbar I_feel_well_informed_about_the_C /group=Program__select_one_ groupdisplay=cluster stat=percent;

For future reference, many people will not download attachments, and so the output you want to show us should be pasted directly into your message.

--
Paige Miller
Rick_SAS
SAS Super FREQ

If you add

STAT=percent

to the VBAR statement, the Y axis will show percentages.

 

Kyra
Quartz | Level 8

Hi,

 

STAT=percent gives me percentage out of total frequency.

I am interested  in getting percentage out of total who agreed, So the sum of 3 bars in agreed should be 100%,

the sum of total who disagreed should be 100% and the sum of neutral should be 100 too.

 

Thanks,

 

 

Rick_SAS
SAS Super FREQ

See the article, "Construct a stacked bar chart in SAS where each bar equals 100%". The article discusses a stacked bar chart, but you can use the same code and change the GROUPDISPLAY= option to CLUSTER

 


proc sort data=sashelp.cars(where=(Type^="Hybrid")) out=cars;
by Origin;                     /* sort X categories */
run;
 
proc freq data=cars noprint;
by Origin;                    /* X categories on BY statement */
tables Type / out=FreqOut;    /* Y (stacked groups) on TABLES statement */
run;
 
title "100% Stacked Bar Chart";
proc sgplot data=FreqOut;
vbar Origin / response=Percent group=Type groupdisplay=cluster;
xaxis discreteorder=data;
yaxis grid values=(0 to 100 by 10) label="Percentage of Total with Group";
run;
Kyra
Quartz | Level 8

Thank you very much for reply! I tried the following code;

 

proc sort data=red.survey2 out=red.informed;
by I_feel_well_informed_about_the_C;
format I_feel_well_informed_about_the_C $category. ;
run;
proc freq data=red.informed noprint;
by I_feel_well_informed_about_the_C;
tables Program__select_one_ / out=red.FreqOutt;
format I_feel_well_informed_about_the_C $category. ;
run;

proc sgplot data=red.FreqOutt;
vbar I_feel_well_informed_about_the_C / response=Percent group=Program__select_one_ groupdisplay=cluster;
xaxis discreteorder=data;
yaxis grid values=(0 to 100 by 10) label="Percentage of Total with Group";
format I_feel_well_informed_about_the_C $category. ;
run; Following is the output.

However, for the group how is sum more than 100%. I wanted it to be 100% for the 3 bars in agree group. similar for disagree and neutral group.

 

I got the  output-SGPlot3.png

 
 
 

 
Rick_SAS
SAS Super FREQ

I suspect your PROC FREQ call is incorrect. You want to get the row totals. The output should look like the following:

 

data Have;
length Informed Program $8; 
input Informed Program Percent;
datalines;
Agree    Anesth  41 
Agree    Emerg   26
Agree    GenSurg 33
Disagree Anesth  39 
Disagree Emerg    8
Disagree GenSurg 53
Neutral  Anesth  34 
Neutral  Emerg   11
Neutral  GenSurg 55
;

proc sgplot data=Have;
vbar Informed / response=Percent group=Program groupdisplay=cluster;
xaxis discreteorder=data;
yaxis grid values=(0 to 100 by 10) label="Percentage of Total with Group";
run;
Kyra
Quartz | Level 8

Thank you very much! This community is great!!!

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 7 replies
  • 4599 views
  • 1 like
  • 3 in conversation