BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SAS-questioner
Obsidian | Level 7

I have a data looks like below:

ID   sex        education        Reason1        Reason2           Reason3 
1   female         high           A                B                 C
2   male          middle          B                D                 E
3   female       college          A                B                 D

There are 6 reasons (A, B, C, D, E, F), and each person needs to select three out of the six. I want to create a bar chart to display three reasons for demographic groups. In order to make sure all three reason variables are displayed in the bar chart, I convert the data into long format, so all three reason variables were combined like below:

ID     sex      education       reason
1    female       high             A
1    female       high             B
1    female       high             C
2    male         middle           B
2    male         middle           D
2    male         middle           E
3    female       college          A
3    female       college          B
3    female       college          D

I used the code like below to generate the bar chart, so it displays the frequency of each reasons for different demographic groups:

proc sgplot data=long;
	vbar sex / group=reason groupdisplay=cluster;
	xaxis label='Sexl';
run;

However, since the group size is not balanced, I want to display percentage for each reason instead of frequency. But I am not sure how to do it, could anyone help me with it? Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Anytime you discuss percentages you always want to define which values are numerator/denominator of the value you want to display. I am not sure which you want and so it might be that additional manipulation of the data is actually needed.

 

However to change a plot from a frequency plot to percent may be as simple as:

proc sgplot data=long;
	vbar sex / group=reason groupdisplay=cluster
                   stat=percent
                      ;
	xaxis label='Sexl';
run;

to display the percent at the end of the bar add the DATALABEL option on the VBAR statement.

 

If you want the percents to be within the Group you might use:

proc sgplot data=long  pctlevel=group;
	vbar sex / group=reason groupdisplay=cluster;
	xaxis label='Sexl';
run;

If neither of those is what you expect then provide some details.

 

View solution in original post

1 REPLY 1
ballardw
Super User

Anytime you discuss percentages you always want to define which values are numerator/denominator of the value you want to display. I am not sure which you want and so it might be that additional manipulation of the data is actually needed.

 

However to change a plot from a frequency plot to percent may be as simple as:

proc sgplot data=long;
	vbar sex / group=reason groupdisplay=cluster
                   stat=percent
                      ;
	xaxis label='Sexl';
run;

to display the percent at the end of the bar add the DATALABEL option on the VBAR statement.

 

If you want the percents to be within the Group you might use:

proc sgplot data=long  pctlevel=group;
	vbar sex / group=reason groupdisplay=cluster;
	xaxis label='Sexl';
run;

If neither of those is what you expect then provide some details.

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 1 reply
  • 813 views
  • 2 likes
  • 2 in conversation