SAS Tips- Charm of PROC GCHART
People always complain that SAS is rigid and prim, treating is as a software in dull looking and filled with tedious codes.
I think the reason might be the lack of graphs.
In fact, SAS could generate very beautiful graphs and charts.
PROC GCHART may be one of the most useful statements to make graphs. There are many types of charts.
Let's have a simple example:
data tt; input name $ course $ grade; cards; Jim English 44 Jim Maths 88 Jim History 94 Kate English 93 Kate History 74 Kate Maths 30 Nick English 99 Nick History 40 Nick Maths 85 ;
Here is part of the students' transcripts. Now we'd like to make a block chart to have a intuitive view of it.
The simplest code is here.
"data=" option specify which dataset to display, and block statement tells SAS which variable we want to show.
proc gchart data= tt; block name; run;
We find the chart shows the frequency of each name: 3 (because Jim, Kate and Nick has 3 courses each), not their total grade.
Let' add one more option.
"sumvar=" tells SAS to calculate the sum of the specified variable.
proc gchart data= tt; block name/ sumvar= grade; run;
What about bar chart? Just replace the block statement above with vbar statement. Like this.
proc gchart data= tt; vbar name/ sumvar= grade; run;
However, we don't want the axis to be that long and the tick marks to be that dense. Moreover, we may want to change the labels, the color of bars or lines, and even some more detailed settings.
These could all be controlled in axis statement.
Just like format, or template, we can make our own style of axis, using axis statement.
axis1 color= green order= (0 to 300 by 50) minor= (n= 1) label= ('Total grade'); ; proc gchart data= tt; vbar name/ sumvar= grade raxis= axis1; run;
We specify an axis style named axis1, then apply it to the chart.
"color" here is axis color. If you'd like to change the colors of bar or text, more options in vbar statement would help.
'order' controls the tick marks. Since total grade would not greater than 300, 0 to 300 is enough. 'by 50' is major scale. 'minor' option controls minor tick mark. Here we don't like it to be so dense, so we make it 1, meaning within each major tick mark there is only one minor tick mark.
This is just a simple case. Some more complicated case may be discussed in the future. Anyway, SAS, with a powerful chart making function, is not tedious, but full of fun!