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!
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.
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.
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.
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.