BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
kz_
Quartz | Level 8 kz_
Quartz | Level 8

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%
;

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@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).

View solution in original post

12 REPLIES 12
kz_
Quartz | Level 8 kz_
Quartz | Level 8

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%

 

Reeza
Super User
If you put in fake values then plot that data wouldn't that be a user defined height?
How do you want to user define the heights? Is that via prompt or some file that's provided? Will all data be refreshed or will you expect users to manually enter all data each month?
kz_
Quartz | Level 8 kz_
Quartz | Level 8
What I meant was that I have already calculated the percents for each month but I don't know how to create a plot from a summary table.
Reeza
Super User
Which variable is that in the data you posted?
kz_
Quartz | Level 8 kz_
Quartz | Level 8
It's not in the data set - it's in the description. The percents that I calculated are in a separate, summary dataset.

I can add it, but I will have to add more data.
kz_
Quartz | Level 8 kz_
Quartz | Level 8
Maybe you can suggest how to create the percents as fake values. It seems that when making a bar chart, SAS counts the rows.
Reeza
Super User
I'm sorry, your question is too unclear for me to answer. I don't know what your input data looks like or what you want to graph. Hopefully someone else will be able to assist you.
ballardw
Super User

@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).

kz_
Quartz | Level 8 kz_
Quartz | Level 8
This almost works, but there is the same problem I was having. I want all the bars for group 1 to be adjacent to each other (I used the word 'cluster' in the question - maybe that was confusing).
kz_
Quartz | Level 8 kz_
Quartz | Level 8
In any case, I have decided on a different display of the data, so I will accept this as the solution.
kz_
Quartz | Level 8 kz_
Quartz | Level 8
Thanks, that seems like a useful link, but it doesn't include what I'm looking for.

SAS Innovate 2025: Register Now

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!

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
  • 12 replies
  • 1657 views
  • 0 likes
  • 3 in conversation