StatsSA's CPI inflation statistics

Whilst the Headline Consumer Price Index (CPI) is the price index that is most commonly referred to (it is the index for all urban areas, for all items), there are in fact many Consumer Price Indicies, across various different geographical regions, or types of goods.

Scrape and tidy the latest StatsSA inflation data

Do you need the latest inflation index, to show your analysis at the most recent level of prices? You can use this R code to scrape the latest price indicies from StatsSA's website, and tidy the dataset. If you cannot run R, you can use the tidy Excel file that I keep updated in my Dropbox storage, or you can pull the .csv file programmatically. (I keep it updated automatically, by getting my Windows Task Scheduler to run the R script in a .bat file once a day.) I try put the future CPI publication dates into the UCT Economics Society's online calendar.

For example, in Stata, type this at the beginning of your .do file:

import delimited using ""
keep if region=="All urban areas"
scalar latest_CPI = allitems in -1
scalar latest_CPI_month= month in -1
scalar latest_CPI_year= year in -1

Then merge the data with your dataset. Or, in R, type:

CPI <- read_csv("") %>%
select(1:5) %>%
filter(Region=="All urban areas")

# Headline CPI is the CPI for all urban areas, all items.
latest.CPI <- setNames(
CPI$`All Items`[nrow(CPI)],


If you would like to easily graph the various different types of inflation, have a look at this Excel file I created. Comments? I don't keep this file up-to-date with the latest indicies, but I aim to use the concept to create a similar Shiny app. I am currently keen on implementing this idea.

Whilst StatsSA typically reports year-on-year statistics, I also smooth the inflation rates over longer periods, with the moving trend average. That is, the OLS slope of log CPI, over rolling windows. So, each point on the smoothing function represents a slope average for the time window on either side of it. In Excel, the formula would be

=( exp( slope(<log CPI vector>, <time vector>) )^(time intervals per year) -1 ) *100%

For example, the following graph shows the trend of Headline CPI (the end of the period does not use future dates, so smaller windows are used there so that the trend is plotted up to the present time).