Reputation: 1262
Is there any way to specify formats directly for axis values and data labels? As far as I can tell, it uses whatever format is applied to the dependent variable.
Example:
data sample;
input group $ number;
format number dollar6.1;
cards;
A 55.2
B 20.3
C 47.1
D 43.2
;
run;
axis1 minor=none order=0 to 60 by 10;
proc gchart data=sample;
vbar group/ type=sum sumvar=number sum levels=all raxis=axis1;
run;
If I set the format to dollar6.1 then the axis labels have an unecessary decimal (0.0, 10.0, 20.0, etc.)
But, if I set the format to dollar6.0, then the labels on the tops of each bar are missing the decimal that I would like to show.
Any way to specify formats independantly for either of these?
Upvotes: 2
Views: 4002
Reputation: 63424
I don't believe you can control the formats separately; you have limited kinds of control as far as time axis, log axis, etc., but otherwise no control over the numeric format.
What you can do is one of two things. At least in SGPLOT, you can create a secondary variable with a different format, and produce an empty graph (or an identical copy of your bar chart but with no label) using the variable formatted how you want the axis formatted; then produce the chart with the second, otherly formatted variable.
Secondly, you can assign explicit values to the axis. Rather than using the automatic values arising from your data, you can just use VALUE= to overwrite the labels placed on the tick marks. This isn't optimal if you have a varying axis (ie, you produce twenty of these with different axis amounts or whatnot), but if it's a fixed axis then you can probably get away with this. Look at the AXIS statement in GChart for more information.
How you'd do the first option:
data sample;
input group $ number;
format number dollar6.1;
axis_number = number;
format axis_number dollar6.0;
cards;
A 55.2
B 20.3
C 47.1
D 43.2
;
run;
proc sgplot data=sample;
vbar group /response=axis_number;
vbar group /response=number datalabel;
yaxis label='Number (sum)';
run;
That creates the bar chart twice, once with axis_number which then defines the axis, and once with number which defines the labels.
Upvotes: 2
Reputation: 2762
You can do this sort of thing using an annotate dataset. I'd give a better explanation of this if I had a solid understanding of how it works, but I use it so rarely that it's usually more of a trial-and-error process:
data sample;
input group $ number;
format number dollar6.0;
cards;
A 55.2
B 20.3
C 47.1
D 43.2
;
run;
Create anno
dataset. I pulled this from the link above and got rid of extraneous stuff. Set [function]='label'
, [position] = '2'
to place the labels above the bars, xsys = 2'
and ysys = 2
to base the coordinates on the data values. size
and style
control the font.
midpoint=group
puts the labels on the bars, y=number
makes the y coordinate of the label equal the height of the bars, and text
is where you specify the value and format of your label.
data anno;
length function style $12;
retain function 'label' size 1 position '2'
xsys '2' ysys '2' style 'Albany AMT';
set sample;
midpoint=group;
y=number;
text=put(number,dollar6.1);
run;
Make your chart using your current code, but removing the sum
and inserting annotate=anno
.
axis1 minor=none order=0 to 60 by 10;
proc gchart data=sample;
vbar group/ type=sum sumvar=number annotate=anno levels=all raxis=axis1;
run;
Upvotes: 2
Reputation: 8513
If you're running 9.2 or later, and are happy to use the Graphics Template Language (GTL) then you can do it like this:
Add a new column to your data that rounds the value:
data sample;
input group $ number;
format number dollar6.1;
axisval=round(number,1);
cards;
A 55.2
B 20.3
C 47.1
D 43.2
;
run;
Define the chart:
proc template;
define statgraph mychart;
begingraph;
layout overlay;
barchartparm x=group y=axisval / datalabel=number;
endlayout;
endgraph;
end;
run;
Render the chart using the data we created earlier:
proc sgrender data=sample template=mychart;
run;
The trick here is using the datalabel=
option of the barchartparm
statement to specify which column contains the values for the labels. There may be some other ways to do this using the GTL and specifying formats but this seemed pretty straightforward to me.
The GTL is included in Base SAS 9.2 onwards I believe.
Upvotes: 1