By on 6.6.22 in Story Recipe

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.

Creating Custom Strings

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:,B11003_001E&for=county:*&in=state:37

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., ...&key={yourAPIkeyhere}.

Download Stata .do

You can click below to download a Stata .do file that uses the censusapi package to download data. The file looks like this:


Click to download .do file

Republish our content for free under a Creative Commons license.

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

Featured projects

Your support is critical to our mission of measuring, understanding, and predicting population change and its impact. Donate to Carolina Demography today.