Story Recipe: Using the Census API
Searching and Parsing Variables from the American Community Survey (and Other Datasets)
This piece was originally published on ObservableHQ. Evan Galloway is a research associate with the program on Health Workforce Research and Policy at the Cecil G. Sheps Center for Health Services Research.
The Census Data API is an incredible resource that makes a huge universe of data available programmatically. However, it can be hard to find the exact variables you need for your query. Also, commonly reported statistics are usually aggregates of several variables in, say, the American Community Survey. For instance, if you want to find out the percentage of people in a county who did not finish high school, you need to add up a bunch of different variables and divide that value by the group/concept total.
The Census Data Explorer is handy as a starting point for finding variable families. In my case, since I am working off of this project, I already know that I need data from family B15003 – EDUCATIONAL ATTAINMENT FOR THE POPULATION 25 YEARS AND OVER.
But I still need a way to search and extract variable codes and labels to aggregate for calculating certain metrics. I could copy and paste from the 2019 ACS variable list, but I’d like to avoid introducing errors through copying and pasting, and I want to format the labels a bit. Also I’m doing my analysis in Stata so I want to generate some statements for loading and labeling the variables.
So let’s get started by specifying a dataset from the list of datasets at the Census website. We want the
API Base URL for the 2019 5 year ACS.
You can edit the text box to add another dataset from the list of Census datasets, though I have only used this with recent ACS datasets, so you may need to modify the code, especially the code that parses the labels for text output.
Now what? Well, I needed to create a bunch of label statements to label these otherwise incomprehensible codes in Stata, so I wrapped these along with the code for loading variables into a text file that you can download. (See
Download .do file button below.)
However, if you don’t use Stata you might need some to generate some other strings. For instance, you might want to create a URL for loading the data from the Census. If you want to, say, load county data for North Carolina from the 2019 5 year ACS. The query looks like this:
and the same query with all variables you selected from the table looks like this:
No variables selected in table.
Note that depending on the size and frequency of reqeusts you may need to sign up for an API key, and append it to your query, e.g.,
You can click below to download a Stata .do file that uses the censusapi package to download data. The file looks like this:
Need help understanding population change and its impacts on your community or business? Carolina Demography offers demographic research tailored to your needs.
Contact us today for a free initial consultation.Contact Us
Categories: Story Recipe
The Center for Women’s Health Research (CWHR) at the University of North Carolina School of Medicine released the 12th edition of our North Carolina Women’s Health Report Card on May 9, 2022. This document is a progress report on the…
Dr. Krista Perreira is a health economist who studies disparities in health, education, and economic well-being. In collaboration with the Urban Institute, she recently co-led a study funded by the Kate B. Reynolds Foundation to study barriers to access to…
Our material helped the NC Local News Lab Fund better understand and then prioritize their funding to better serve existing and future grant recipients in North Carolina. The North Carolina Local News Lab Fund was established in 2017 to strengthen…
Your support is critical to our mission of measuring, understanding, and predicting population change and its impact. Donate to Carolina Demography today.