Hi, I am a beginner at SAS/GRAPH and I need to create a clustered bar graph. Below is the data. I have been looking at codes, but I am not able to understand how to represent the below values in the code. I was looking at proc sgplot.
Time_periods |
Total |
A |
B |
C |
D |
Time_one |
4304 |
68.61 |
83.02 |
53.72 |
32.99 |
Time_two |
75743 |
61.16 |
83.02 |
56.86 |
30.30 |
Time_three |
8965 |
64.08 |
86.79 |
58.39 |
25.41 |
Time_four |
4824 |
65.78 |
86.53 |
57.36 |
28.86 |
I am not able to copy-paste the chart from excel, so I have a document for reference to the figure. I basically need the intervals on the x-axis and the variables A, B, C and D represented in each time_periods. The y-axis is upto value of 100%. The values in A, B, C and D are in percents. I need these color coded. I also need the totals provided by the variable 'Total' displayed below each interval. Please advise as soon as possible. Thanks.
Hi
To get the graph as in the document attached, you can use the code below. However it is not clear what "Total" actually displays.
data have;
infile cards;
input
Time_periods : $32. Total
A B C D
;
cards;
Time_one 4304 68.61 83.02 53.72 32.99
Time_two 75743 61.16 83.02 56.86 30.30
Time_three 8965 64.08 86.79 58.39 25.41
Time_four 4824 65.78 86.53 57.36 28.86
;
data want;
set have;
array colValues{*} total a b c d;
do i = 1 to dim(colValues);
xValue = vname( colValues{i} );
groupValue = time_periods;
value = colValues{i};
output;
end;
keep xValue groupValue value;
run;
proc sgplot data=want noborder;
vbar xValue / group=groupValue groupdisplay=cluster response=value grouporder=data ;
yaxis values=(0 to 100 by 10) display=(nolabel);
xaxis values=("Total" "A" "B" "C" "D") display=(nolabel);
keylegend / location=outside position=right noborder title=" ";
run;
If you need something else please provide an example on what the graph should look like. Also the number do not add up to a 100%
Hi
To get the graph as in the document attached, you can use the code below. However it is not clear what "Total" actually displays.
data have;
infile cards;
input
Time_periods : $32. Total
A B C D
;
cards;
Time_one 4304 68.61 83.02 53.72 32.99
Time_two 75743 61.16 83.02 56.86 30.30
Time_three 8965 64.08 86.79 58.39 25.41
Time_four 4824 65.78 86.53 57.36 28.86
;
data want;
set have;
array colValues{*} total a b c d;
do i = 1 to dim(colValues);
xValue = vname( colValues{i} );
groupValue = time_periods;
value = colValues{i};
output;
end;
keep xValue groupValue value;
run;
proc sgplot data=want noborder;
vbar xValue / group=groupValue groupdisplay=cluster response=value grouporder=data ;
yaxis values=(0 to 100 by 10) display=(nolabel);
xaxis values=("Total" "A" "B" "C" "D") display=(nolabel);
keylegend / location=outside position=right noborder title=" ";
run;
If you need something else please provide an example on what the graph should look like. Also the number do not add up to a 100%
Here is an example of the chart I need. I tried copy-pasting but again it doesnt work. In the link on the x-axis , I would like to display the total number of people represented by the variable 'total' in my example data. Is there a way to do it? Thanks.
Hi
just remove the total variable from the ARRAY statement, and you get the graph you need. In the data provided, you only have one total value related to Time_one, Time_two etc., but you need it for A,B etc. so the data has to be changed.
One can use the XAXISTABLE to add addional information below the X axis.
Adding images is realy easy, as long as you have an image in the clipboard, just use Ctrl-V to insert the image. As an alternative you can use "Insert/edit Image" tool in the editor to insert saved images.
Thanks @BrunoMueller. I got the chart I needed. However, I realized I had it the other way round in the excel sheet. i.e I needed the time_period on the x-axis. I made the necessary changes to the code.
So thats the reason I need the 'Total' for each time_period displayed below the label in parentheses. I will check out the XAXISTABLE for it.
Also, how can change the name of the labels on the x-axis and legend? Do I have to accomplish it by renaming before the array or is there a way to accomplish it on proc sgplot? Any leads would be helpful as time is short.
Thanks and for the tip on attaching the charts.
Hi Bruno_sas and all,
I tried to look up for the XAXISTABLE to add information to the x-axis and I wasn't abe to get any.
(1) I need to add the total number of people represented in each category on the x-axis in parentheses.
(2) Also, how do I change the layout to landscapeso so I can add the 'N' below each x-axis category as the chart is wide and the category labels are all slanting.
(3) I also want to rename the legend lables and the x-axis category labels.
(4) Is there a way to get rid of the box boder of the chart.
I tried looking in options and was unable to locate the same. Please advise.
Below is the code:
proc sgplot data=figure3 noborder;
vbar xValue / group=groupValue groupdisplay=cluster response=value grouporder=data ;
yaxis values=(0 to 100 by 10) label="Proportion XXXXX(%)";
xaxis values=( "Time_one" "Time_two" "Time_three" "Time_four") display=(nolabel);
keylegend / location=outside position=right noborder title=" ";
run;
Using Bruno's code, I modified it to use a "barparm" instead of a "bar", since there was no summarization involved. This change gives you some extra flexibility for overlay types and variable combinations. I also changed the orientation, as the bars are too close together to display the values in a vertical orientation. I used a custom format to change your legend values. You can do the same thing for your axis value, or use the VALUESDISPLAY option. This might not be exactly what you want, but it should give you some ideas.
Hope this helps!
Dan
data have;
infile cards;
input
Time_periods : $32. Total
A B C D
;
cards;
Time_one 4304 68.61 83.02 53.72 32.99
Time_two 75743 61.16 83.02 56.86 30.30
Time_three 8965 64.08 86.79 58.39 25.41
Time_four 4824 65.78 86.53 57.36 28.86
;
data want;
set have;
array colValues{*} total a b c d;
do i = 1 to dim(colValues);
xValue = vname( colValues{i} );
groupValue = time_periods;
value = colValues{i};
output;
end;
keep xValue groupValue value;
run;
proc format;
value $custom_legend "Time_one"="custom 1"
"Time_two"="custom 2"
"Time_three"="custom 3"
"Time_four"="custom 4";
run;
ods graphics / noborder;
proc sgplot data=want noborder;
format groupValue $custom_legend.;
xaxis values=(0 to 100 by 10) display=(nolabel);
yaxis values=("Total" "A" "B" "C" "D") display=(nolabel);
hbarparm category=xValue response=value / group=groupValue groupdisplay=cluster grouporder=data ;
yaxistable value / class=groupValue classdisplay=cluster;
keylegend / location=outside noborder title=" ";
run;
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.