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

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.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
BrunoMueller
SAS Super FREQ

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%

View solution in original post

9 REPLIES 9
BrunoMueller
SAS Super FREQ

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%

dr2014
Quartz | Level 8
@BrunoMueller, thanks I will try it. Also,the 'total' variable is the number of people in the time_period that needs to displayed on the x-axis under each clustered bars for its respective time_period. I was unable to coy-paste the chart on the post. For some reason it didn't allow me to do it. So i attached an excel sheet for the same. Are you able to open it? Please let me know.
dr2014
Quartz | Level 8
Yes, the numbers do not add up to 100% because it excludes the 'missing' category during calculations.
dr2014
Quartz | Level 8

https://image.slidesharecdn.com/unit8presentingdatainchartsgraphsandtables-141121043601-conversion-g...

 

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.

BrunoMueller
SAS Super FREQ

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.

 

dr2014
Quartz | Level 8

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.

dr2014
Quartz | Level 8

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;

 

DanH_sas
SAS Super FREQ

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;
dr2014
Quartz | Level 8
Hi DanH_sas, thank you I tried it and I can understand its value. I will definitely remember to use it, but now I am required to display the chart in the previous manner. Also any help on displaying the 'N; below the time periods would be appreciated!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

Discussion stats
  • 9 replies
  • 7871 views
  • 0 likes
  • 3 in conversation