In this video, you learn how to create bar charts
with Base SAS. In this video, you learn how to create bar charts
with Base SAS. You can quickly and easily generate graphic
visualizations to discover or illustrate patterns in
your data. Let’s start with a horizontal bar chart to look at
how body fat percentage changes as people
age. I’m going to use the SAS Explorer window
to drill into the libraries, and then
double-click on the sasuser library, and then I’m going to double-click on the
bodyfat3 data set to open the data we are going
to be plotting. Here we have one row per subject. Each subject is assigned to an age group, and
for each we have measures of percent body fat
and other metrics. I’m going to go ahead and close this table
viewer, and then I’m going to highlight this first
step and run it. I’ll click the Run button. The graph is displaying in the results viewer. So let me talk about how this program created
the graph. The PROC SGPLOT statement starts the
procedure and names the bodyfat3 SAS data
sets in the sasuser library as the data set. Then the HBAR statement is used to generate
the horizontal bar chart you see here with one
bar per value of the agegroup variable. After the slash, many options are available. The RESPONSE=option says each bar will
represent a summary statistic for the
pctbodyfat1 variable. And the STAT=option makes the statistic
mean. At this point, the visualization might tell you all
you want to know. But for presentation purposes, there are other
things we can do to enhance the appearance. In this next step, right here, what I’m doing is
adding fill attributes on the HBAR statement
to change the color of the bars to light blue. Let me go ahead and run this one, and you’ll
see the change in the colors of those bars in the
plot. So, right now, they are gray, but when I submit,
the color of the bars changes to blue. A little bit further down, I’ve got another version
of the program. I can do a nicer job with this one by adding
these AXIS statements to specify some axis
labels and the font size for the axis. So let me go ahead and submit this one. I’ll
click the Submit button. I’ve got some larger fonts and some better text
for my axis labels. Here on the AXIS statement, the LABEL=is
controlling the text for the axis labels, and then these label attribute options are
allowing me to set the font size. Looking at this graph, it seems that percent
body fat does increase with age, but there is a
decrease for the 51-60 age group. So maybe that’s an artifact of low sample size
in that group. Let’s look at the number of
subjects in each age group instead. For this, I’ve simply, in this next step, removed
only the RESPONSE=option to simply look at
frequency counts by agegroup. I also modified the text for the label in the XAXIS
statement. I’m going to highlight this next step and then
click the Run button to execute. I can now see that the sample size for the
groups ranges from about 8 to over 80. So, I’m
not sure. Not a huge sample size, but not real small
either for most groups. With PROC SGPLOT, it is very easy to overlay
multiple plots if they have a common axis. So next I’ll reuse the earlier plot of mean percent
body fat, and I’ll overlay a line plot of the
frequencies for each subject. That’s in the next step if I scroll down. What’s new here is the HLINE statement and
the X2AXIS statement. Let me execute this first and then I’ll talk about
how it’s generating the plot. The HBAR statement is generating the mean
percent of body fat for each age group. And the HLINE statement is overlaying a line
plot for the frequncies for each age group. That’s possible because they are sharing the
same group axis. The X2AXIS statement enables me to scale the
two statistics separately so that the plots
overlay nicely. Another way to do this is to use HBAR
statements instead of an HLINE to create bars
for the frequencies. That’s in this next step. I’ve got a second HBAR statement instead of an
HLINE statement and I’m requesting a
STAT=FREQ. The other thing I’ve done is set the bar width to
.5 so that the bars for the frequencies are narrower and overlay, and I can see both bar
charts at once. I’ll go ahead and submit this to see what it looks
like. The narrower red bars are the frequencies and
the wider blue bars are the mean percent body
fat values. In addition to bar charts and line charts, a host
of other chart types are available. If you prefer
vertical bars, not much needs to change. In this next step, HBAR becomes VBAR, and
the options for the YAXIS and XAXIS statements
need to be reversed. So this will produce the vertical bar chart for the
mean percent body fat for each age group. Finally in this next step, I’ll show you the VBOX
statement, which creates box plots. I’ll scroll down to the final step. So with the VBOX statement, I’m generating
plots for each age group to represent the
distribution of percent body fat. I’m using the CATEGORY=AGEGROUP to
generate separate box plots for each of the age
groups. The other statements are being used in the
same way as before. I could keep going, but I
think you get the idea. In this demonstration, you have seen how to use
the SGPLOT procedure in Base SAS. Keep PROC SGPLOT in mind if you want a
quick way to create bar charts, line charts, box
plots, and a host of other visualizations. It is easy to overlay various plots as well, and
there are numerous options to enhance the
presentation. It is easy to overlay various plots as well, and
there are numerous options to enhance the
presentation. Thanks for your interest in SAS.