Data visualization with SAS programming

Help formatting gchart

Reply
N/A
Posts: 0

Help formatting gchart

I almost have my hbar chart where I need it to be, with the following exceptions:

1) My bars are "stacked" using a subgroup variable - I have each subroup labeled with its percent value. However, I cannot figure out how to get the percent value centered within its area of the bar. Here is the code used to label the subgroups - I have the code "annotate=barlabel" as an option in my hbar statement:

data barlabel; length color style $ 8; retain color 'black' when 'a' style 'swissb' xsys ysys '2' position '[less than sign]' size 2 hsys '3'; set stats;
midpoint=s;
subgroup=response;
text=left(put(percent,5.));
run;

*Note: I needed to indicate the "less than" sign with words above because otherwise it would cut my post short.

2) I cannot, for some reason, change the label or values of the vertical axis - I am trying to change the font and use right justify, but nothing happens. Here is the rest of the code I am using:

legend1 across=3 shape=bar(3,2) label=none position=(bottom outside center)
value=(font='arial' h=8pt 'Positive Top 3' 'Neutral Middle 3' 'Negative Bottom 3');
proc format; picture pctfmt (round) 0-high='000%'; run;

proc gchart data=stats;
axis1 order=(0 to 100 by 10) minor=(n=1) label=none value=(font='arial' height=8pt);
axis2 label=(font='arial' height=8pt justify=right) value=(font='arial' height=8pt );
hbar s/discrete sumvar=percent subgroup=response type=sum nostats width=3
vref=100 coutline=black frame raxis=axis1 legend=legend1 annotate=barlabel;
format percent pctfmt.;
run; quit;


If anyone has any suggestions, I would be extremely grateful.

Thanks in advance!
SAS Super FREQ
Posts: 890

Re: Help formatting gchart

What version of SAS are you using?
N/A
Posts: 0

Re: Help formatting gchart

SAS 9.1
SAS Super FREQ
Posts: 890

Re: Help formatting gchart

I modified your test to use sashelp.class, just as an example. The key do doing what you want is to use proc summary to pre-compute your data and use X and Y in the annotate dataset instead of MIDPOINT and SUBGROUP. That way, you can compute the label locations based on the summarized data. Here the example:

proc summary data=sashelp.class nway;
class age sex;
var height;
output out=class sum=;
run;

data barlabel;
length color style $ 8;
retain color 'black'
when 'a'
style 'swissb'
xsys ysys '2'
position '4'
size 2
hsys '3'
previous 0;
set class;
by age;
y=age;
x=previous+(height/2);
if (last.age) then previous=0;
else previous=previous+height;
text=left(put(height,5.));
run;

proc print data=barlabel; run;

legend1 across=3 shape=bar(3,2) label=none position=(bottom outside center)
value=(font='arial' h=8pt 'Positive Top 3' 'Neutral Middle 3' 'Negative Bottom 3');

proc format; picture pctfmt (round) 0-high='000%'; run;

proc gchart data=sashelp.class;
axis1 /* order=(0 to 100 by 10) */ minor=(n=1) label=none value=(font='arial' height=8pt);
axis2 label=(font='arial' height=8pt justify=right) value=(font='arial' height=8pt );
hbar age/discrete sumvar=height subgroup=sex type=sum nostats width=3
vref=100 coutline=black frame raxis=axis1 legend=legend1 annotate=barlabel;
run; quit;
N/A
Posts: 0

Re: Help formatting gchart

Dan, I can't thank you enough for your reply. I now have the labels centered on the corresponding bars.

However, I am running into a problem because my variable that is like "age" in your example is a categorical variable and I can't figure out how to label it with words. It has to be numerical to make your solution to the centered numbers work.

There must be something wrong with my axis code?

What is the vertical axis on my horizontal bar chart called? Is there a way to format the numbers to words? I apologize if I am not explaining this clearly...

Thanks in advance.
SAS Super FREQ
Posts: 890

Re: Help formatting gchart

If your midpoint data is strings, create a numeric column that assigns a numbers to the strings. Then, create a user-defined format that maps the numbers back to strings. Use the numeric column in the datastep and in the gchart procedure, using a FORMAT statement in the GCHART procedure to assign the format to your numeric column.

Thanks!
Dan
N/A
Posts: 0

Re: Help formatting gchart

Dan - thank you so much for your help!
Ask a Question
Discussion stats
  • 6 replies
  • 639 views
  • 0 likes
  • 2 in conversation