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 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 226 views
  • 2 likes
  • 2 in conversation