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

I have a set of data which consists of different ethnic groups and their feedback in a survey. I want to plot their responses by groups but the group size is different by the ethnic groups. So I want to plot the percent of frequency using the individual group total instead of the whole population in the survey. Currently I use the group feature in SAS to plot but that really doesn't represent the actual picture. Here's an example of the code:

 

title "Were you satifisfied with your trip?";

proc sgplot data= sasdata.cruise;
yaxis label= "Percent of Frequency";

vbar Q156_1/group=Q100 groupdisplay=cluster stat=percent datalabel ;
XAXIS discreteorder=unformatted DISPLAY=(NOLABEL);
run;
quit;

 

Thanks in advance.

 

Kathie

SGPlot12.png

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

Calculated it before PROC SGPLOT .

 

data have;
 set sashelp.heart;
run;


proc sql;
create table want as
select bp_status,Weight_Status,count(*)/
(select count(*) from have where Weight_Status=a.Weight_Status) 
as percent format=percent8.2
 from have as a
  where bp_status is not missing and Weight_Status is not missing
   group by bp_status,Weight_Status ;
quit;

proc sgplot data=want;
vbar bp_status/group=Weight_Status groupdisplay=cluster response=percent nostatlabel datalabel;
run;

Ksharp_0-1647951048900.png

 

View solution in original post

4 REPLIES 4
ballardw
Super User

Try adding the PCTLEVEL= Group to the VBAR statement if you want percentages calculated using the Group total.

katslau
Fluorite | Level 6
That didn't quite do what I want. It's giving me the % breakdown among the ethnic groups with one type of response, not by ethnic groups. What I want is that all responses should add up to 100% within one ethnic groups across the graph.
ballardw
Super User

@katslau wrote:
That didn't quite do what I want. It's giving me the % breakdown among the ethnic groups with one type of response, not by ethnic groups. What I want is that all responses should add up to 100% within one ethnic groups across the graph.

Your initial problem statement is not very clear. You stated "So I want to plot the percent of frequency using the individual group total instead of the whole population in the survey."

You did not say what 'individual group' you wanted.

 

I think that you will likely need to summarize your data with another procedure before plotting, likely with Proc Freq creating an output data to get the percentages you want and then use a VBARPARM.

Ksharp
Super User

Calculated it before PROC SGPLOT .

 

data have;
 set sashelp.heart;
run;


proc sql;
create table want as
select bp_status,Weight_Status,count(*)/
(select count(*) from have where Weight_Status=a.Weight_Status) 
as percent format=percent8.2
 from have as a
  where bp_status is not missing and Weight_Status is not missing
   group by bp_status,Weight_Status ;
quit;

proc sgplot data=want;
vbar bp_status/group=Weight_Status groupdisplay=cluster response=percent nostatlabel datalabel;
run;

Ksharp_0-1647951048900.png

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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