Hi, everyone.
Welcome to the Analyzing Census Data in SAS Studio tutorial series.
In the last two videos, I downloaded data on median home values in each state,
then imported this data into SAS Studio.
Now that I have a SAS table that I can use,
I'll show you how you can create a bar chart
using the bar chart task to have a visual to compare median
home values across states.
We'll also look at editing the code
generated by the bar chart task
to further enhance the results.
If you want to follow along with me,
make sure you watch the introduction video
to set up your environment and tutorial data.
You can find a link to it below, or on the main tutorial page.
Let's get started.
So in SAS Studio, I have the census.medianhomevalue table opened up.
We're just going to take a quick look at this table before
we jump into the bar chart task.
Now if you didn't follow along with the previous
tutorial where I imported in this data,
no worries at all. You can instead use the
census.medianhomevalue_s table and follow along using that.
Looking at this table here, we have one row
for each state,
and we have this column MedianHome containing those median home values.
What I want to do with the bar chart task is essentially provide a visual of this information,
to make it easier to compare these median home values.
Let's jump into the task.
In the navigation pane, I'm going to expand the Tasks and Utilities section.
And under Tasks, I'm going to expand the Graph category, and then double click Bar Chart.
This opens up the Bar Chart task in a new tab in the work area, and by default, the view is set to
split. So I see the settings on one side and the code and results on the other side. I'm going to
keep that as is, but I'm going to click on the Maximize View button, and that's going to hide
the navigation pane, and just provide me with a little bit more room to work with.
So the first thing I need to do on the settings side,
specifically under the Data tab, is specify my input table.
Now you'll notice that my input table is that census.medianhomevalue is already selected,
but if you don't see that,
click on the Select a Table button,
expand that Census Library,
select the median home value table, or the underscore S version,
and click OK.
On that Data tab, next, a chart orientation, I'll keep this as a vertical bar chart.
So let's move on to our task roles.
Our task roles determine how our columns will be used within the task.
We have a couple of roles here, but take a look at the Category role.
The Category role
has a red asterisk next to it, which means that it is a required role. I have to assign
some sort of column to this role.
Now remember,
I want to create a bar chart with one bar for each state, the height representing that median home value.
Because I want one bar for each state, to my Category role, I'll assign that State column.
So I'll click on the plus sign, which is add a column,
select State,
and click OK.
Next, for my Measure role, that determines the height of the bars.
The default is just a frequency count, but again, I want those median home values.
So from the dropdown list I will select Variable,
and to the Variable role I will assign the MedianHome column.
And click OK.
Now for the statistic that is used, the default is the sum. And we're actually
going to use that statistic because we only have one median home value per state, sum of
one value is just the value itself, so again, we're going to stick with that Sum.
Now there is a lot more options that we can take advantage of in the Bar Chart task,
and we'll explore some of those coming up.
But for now, let's take a look at the Code tab.
This task, the Bar Chart task generates PROC SGPLOT code behind the scenes for us.
And we can see that right here on that
Code tab.
At this point, let's go ahead and give this code a run, let's see what we have so far. So I'll click Run.
Now I'm looking at the bar chart, we have a start here, but we could probably make some
improvements.
All right, I have one bar for each state, the height represents the median home value,
that's good. But I can see that all of my state names are being overlapped with each other.
I might want to expand the width of this graph to make sure that they aren't overlapping.
Maybe I'd like to add in a title, and maybe I can change the label of the y-axis, for example.
To make these changes, I'm just going to go back to the settings side.
Specifically now, we're going to the Appearance tab to make these changes.
First, to change the label on my y-axis, MedianHome,
I'm going to expand the Measure Axis heading, then from the Display label dropdown list,
I will select Custom label. In the label box, less label this Median Home Value.
Next, the title. I'll expand the Title and Footnote heading,
and let's call the Title Median Home Value in 2018.
Finally, the graph size. I'll expand the Graph Size heading, and for the Width,
let's bring that up to 10 inches, and I'll leave the Height at the default 4.8 inches.
Let's run it again, let's see how our bar chart changes.
This is looking a lot better now. My state names are no longer overlapping, there's more room for
it to work with. I see my Median Home Value label on my y-axis and a nice title as well.
Now you might have noticed that when I ran it the second time,
next to my Bar Chart tab, there's actually a warning icon. If you see an icon like this,
it's always a good idea to take a look at your log. So let's jump into the Log tab.
I see there are two warnings here, and they say that the width exceeds available space for
RTF, which is like the word format, and the PDF destination, setting width=8in. So,
what does that mean? Well by default in SAS Studio, our results get generated
in the HTML5, RTF and PDF results.
What these warnings are saying is that for the RTF and PDF format, specifically, that width of 10 was too wide,
so it just set it to 8 automatically.
This does not affect our results though because on the Results tab, we are only viewing the HTML5 results.
But it's always good to make sure you check your
log to see if there's any warnings or errors you might want to be aware about.
I'm going to go back to the Results tab, taking a look at my bar chart again. Now this time,
what I'd like to do is maybe plot the US median home value as a reference line in this bar chart.
I don't know what that US median home value is, and it's not a part of my data.
But, data.census.gov makes it really, really easy to find that information.
So in a new tab, I'll go to data.census.gov.
This is a great resource to access data that the Census Bureau makes available.
And what I'll do this time is in the free form single search bar, I'm going to just type in
exactly what I'm looking for, which is median home value 2018.
Let's see what we get.
Now previously I was looking at tables, but I actually don't even need to look at a table
to get the information that I'm looking for.
You'll notice at the top of the All results page, it immediately tells me
that the median housing value in 2018 was
$229,700. So that was really easy for me to get.
With that in mind, let's go back into SAS Studio.
On the Appearance tab, let's add in that reference line.
Under the Measure Axis heading, which is already expanded,
I'm going to check the check box for Create a reference line, and type in that value, 229,700.
Let's add in a custom label as well, and I will label that as U.S. Median.
Let's give it a run.
So I get pretty much the same bar chart, but now I have that U.S. median home value plotted as a reference line in green.
This makes it pretty
easy to see which states are above the U.S. median home value or below it.
Now let's take that a step
further. Instead of plotting the U.S. median home value as a reference line, maybe I want to filter
my values based off of that, so only plot the states that have median home values exceeding
the U.S. median home value.
To do this, first I'm going to clear that reference line.
So I'll clear to create a reference line checkbox on that Appearance tab.
To filter my data, I'm going to the Data tab.
And right below my input table, I'll click Filter.
In the Filter expression box, I will type in MedianHome --that's the name of the column--
is greater than 229,700.
The syntax that is used here in the Filter expression box is based off of the SAS SQL procedures WHERE clause,
just without that WHERE keyword. If you're interested to learn more about that syntax,
take a look at the tutorial notes, there is a link to the documentation there.
But this is good to go for me so I will click Apply
and let's run it again.
So in my updated Bar Chart, I now see less states.
These are states that have median home values exceeding that $229,700.
So my bar chart's looking pretty good so far, but you'll notice right now that my bars are sorted in
ascending or alphabetical order by the states.
Maybe instead I'd like to sort my bars by the median home value, instead of by the states.
Well, let's take a look at Tasks, see if there's any options for that.
So let's take a look at the Appearance tab.
And under the Category axis heading let's see what we have. So there's an option,
Reverse tick values. What that'll do is sort to bars in reverse or descending order by the
states. Not quite what we want.
And then there is Show tick values in data order. So sort the bars, or the states, in order in which it appeared in the input table itself.
But there isn't an option that lets me sort my bars by those median home values.
If there is an option or setting that you're looking for that you're not finding in the task itself,
what you can do is go in and modify the code that was generated by this Bar Chart task.
If I go to the Code tab
there is that code that we want to modify.
Before I go in and do that, there's one more resource I'd like to show you.
And that is on the Information tab.
I mentioned earlier that the Bar Chart task generates PROC SGPLOT code.
And if you're not very familiar with it, you want to learn more about it,
that information tab actually has a direct link to the documentation that talks about
the SGPLOT procedure. So you might want to check that out.
But for now, let's go back into the Code tab. Now the Code tab, this is a read only copy of the
code. If I want to make a modifiable copy I can just click on Edit.
This opens up a new Program tab that is again, modifiable, so I can make some edits to the code that was generated.
So you might not be familiar with SAS code, but SAS Studio actually has a lot of features available that
make it a little bit easier for you to code. And has some features to help guide you.
So I'd like to show you a couple of those features.
First, let's say in
my code I'm not very familiar with this VBAR statement, but I want to learn more about it.
What you can do, go ahead and right click on the VBAR keyword and select Syntax Help.
The Syntax Help window tells me a little bit about this VBAR statement. So the description
says, it creates a vertical bar chart that summarizes the values of a category variable.
And I can also see the syntax. After that VBAR keyword I list a category variable.
And then after a forward slash is where all of my options are going to go.
So you might be wondering, well, what are some options that are available?
Is there something that I could take advantage to sort my bars by the median home values?
Well, let's see.
I'm going to place my cursor right before the semicolon on that VBAR statement. You just want to make sure you're after
that forward slash. Because again, we're interested in those options.
When I hit the space bar an autocomplete window opens up and shows me all the valid
options for a VBAR statement. And there are a lot of options I can take advantage of here.
The one that I'm interested in is the CATEGORYORDER= option. So I'm going to hit the C
on my keyboard, takes me right to the C's and now you see CATEGORYORDER= is highlighted.
Now I have the Syntax Help window for the CATEGORYORDER= option.
I see that it specifies the order in which the response values, our median home values, are
arranged. That's exactly what we want.
So with the CATEGORYORDER= option highlighted in my autocomplete window, I can just hit the Enter key and now the CATEGORYORDER= option
is automatically added into my program. Which is nice.
Then autocomplete opens up again. Now I can see the valid option values for the CATEGORYORDER= option.
RESPASC and RESPDESC. So response ascending or response descending.
I'm going to single click on response descending so I can read more about it. As you can probably
guess, it sorts the response values in descending order. That's exactly what I want.
So this time I will double click it, and that also adds in that value to my program.
So the Syntax Help window and Autocomplete window can guide you out and help you out as you're
typing inside of a SAS program.
There's one more thing I'd like to add in here, and that is a secondary title explaining the filter that was applied.
So right below the TITLE statement
I'm going to add a TITLE2 statement,
and in quotes, type in the title.
I'll say "States Exceeding the US Median of $229,700".
And then end that statement with a semicolon.
Let's give the program a run.
So here is my final bar chart. Again, only looking at the states that exceed that US median
home value of $229,700. And now it is sorted in descending order by those median home values.
At this point, you may want to share this bar chart in let's say a PDF format for example.
Earlier I mentioned that by default SAS Studio generates our results in the HTML5, PDF,
and RTF formats. In order to access those, there are these buttons on the results tab
to download the results as an HTML file, PDF file, and an RTF or a Word file.
As an example let me show you the PDF file.
Just click on the button, open that up,
and now I have the same exact bar chart but now in a PDF file. Which may make it a little
bit easier to share with your colleagues.
I'll go ahead and close out of my PDF file.
And let's save our work.
I'm going to first exit out of maximized view,
bring back my navigation pane,
and to save the program on the code tab
I'll click on the
Save icon.
Let's save it in the Census Data Analysis folder. I will call this program
Median Home Bar Sort.
And click Save.
Closing out of the program. Let's also save the settings specified in the bar chart task.
Click the save icon here as well.
And in that Census Data Analysis folder let's call this one Median Home Bar, and click Save.
Now you know how to create a bar chart using the Bar Chart task to visualize Census data.
Make sure to try out the practices in the tutorial notes. To learn how to create a stacked bar chart,
check out the challenge practice. You'll even edit the code generated by the bar chart task
to create a stacked bar chart, where each bar equals 100%.
Thanks for watching.