I want to create a bar chart of data that is similar to that below. The bar chart should
- have a separate plot for each type
- have a cluster for each group, which has two bars (one for each month)
- set the height of the bar for each month as the percentage that that group constitutes of the total for that month.
For example, in the first plot for "basic" there would be three clusters. The group 1 cluster would have one bar for 2019-01 of height 100% (it constitutes all the data for that month) and one bar of height 50% for 2019-02.
The group 2 cluster would be 0% for both months.
The group 3 cluster would be 0% for 2019-01 and 50% for 2019-02.
I added these percentages in the 4th column, but it obviously doesn't capture the 0% observations. Also the percentages are repeated for each type-group-month, but there should only be one bar for each combination.
Obviously, I have way more data (groups, types, months) than this.
(Also I don't know how to write the input for the date since my data are already in SAS - this reads the date in wrong, but I can't figure out why.)
Thanks in advance!
data noname;
infile datalines delimiter= ',';
input type $ group yr_mo YYMMD. percent_of_month_by_type;
datalines;
basic, 1, 2019-01, 100%
basic, 1, 2019-01, 100%
basic, 1, 2019-02, 50%
basic, 3, 2019-02, 50%
normal, 1, 2019-01, 50%
normal, 2, 2019-01, 50%
normal, 2, 2019-02, 33%
normal, 3, 2019-02, 66%
normal, 3, 2019-02, 66%
;
@kz_ wrote:
A nice alternative would be a way to construct a bar chart with user-defined heights for the bars. If you take out the repeated rows above, the height is specified by the percent on the right.
basic, 1, 2019-01, 100%
basic, 1, 2019-02, 50%
basic, 3, 2019-02, 50%
normal, 1, 2019-01, 50%
normal, 2, 2019-01, 50%
normal, 2, 2019-02, 33%
normal, 3, 2019-02, 66%
First thing, this data makes more sense than the other.
data noname; infile datalines delimiter= ',-'; input type $ group yr mo percent_of_month_by_type percent.; yr_mo = mdy(mo,1,yr); format yr_mo yymmd.; datalines; basic,1,2019-01,100% basic,1,2019-02,50% basic,3,2019-02,50% normal,1,2019-01,50% normal,2,2019-01,50% normal,2,2019-02,33% normal,3,2019-02,66% ; proc sort data=noname; by type yr_mo group; run; proc sgplot data=noname; by type; vbar yr_mo /response=percent_of_month_by_type group=group groupdisplay=cluster; format percent_of_month_by_type percent6.; yaxis values=(0 to 1 by .25) label="Percent"; run;
Your date informat didn't work because there is no SAS supplied informat to read YYYY-MM data. So read the bits and use the MDY function as on way. Note use of two delimiters so the - is treated as a delimiter.
There is something needed to display "0" bars. One way is to include 0 values and use datalabel option, but no actual bar is visible just the number that would be 0% displayed near the axis.
Another way would be to use a DATTRMAP data set to provide the option of setting specific colors for each group value and to display the value of the group variable even when not present. Search the forum for some examples as this is a specially structured data set with specific variables and I'm too lazy to go into all the details (again).
A nice alternative would be a way to construct a bar chart with user-defined heights for the bars. If you take out the repeated rows above, the height is specified by the percent on the right.
basic, 1, 2019-01, 100%
basic, 1, 2019-02, 50%
basic, 3, 2019-02, 50%
normal, 1, 2019-01, 50%
normal, 2, 2019-01, 50%
normal, 2, 2019-02, 33%
normal, 3, 2019-02, 66%
@kz_ wrote:
A nice alternative would be a way to construct a bar chart with user-defined heights for the bars. If you take out the repeated rows above, the height is specified by the percent on the right.
basic, 1, 2019-01, 100%
basic, 1, 2019-02, 50%
basic, 3, 2019-02, 50%
normal, 1, 2019-01, 50%
normal, 2, 2019-01, 50%
normal, 2, 2019-02, 33%
normal, 3, 2019-02, 66%
First thing, this data makes more sense than the other.
data noname; infile datalines delimiter= ',-'; input type $ group yr mo percent_of_month_by_type percent.; yr_mo = mdy(mo,1,yr); format yr_mo yymmd.; datalines; basic,1,2019-01,100% basic,1,2019-02,50% basic,3,2019-02,50% normal,1,2019-01,50% normal,2,2019-01,50% normal,2,2019-02,33% normal,3,2019-02,66% ; proc sort data=noname; by type yr_mo group; run; proc sgplot data=noname; by type; vbar yr_mo /response=percent_of_month_by_type group=group groupdisplay=cluster; format percent_of_month_by_type percent6.; yaxis values=(0 to 1 by .25) label="Percent"; run;
Your date informat didn't work because there is no SAS supplied informat to read YYYY-MM data. So read the bits and use the MDY function as on way. Note use of two delimiters so the - is treated as a delimiter.
There is something needed to display "0" bars. One way is to include 0 values and use datalabel option, but no actual bar is visible just the number that would be 0% displayed near the axis.
Another way would be to use a DATTRMAP data set to provide the option of setting specific colors for each group value and to display the value of the group variable even when not present. Search the forum for some examples as this is a specially structured data set with specific variables and I'm too lazy to go into all the details (again).
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.