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

 

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