Fixing FIPS – SPSS solves today’s annoyance

I have long suspected that the main role of other people on this earth is to annoy me. Take FIPS codes.

FIPS stands for “Federal Information Processing Standard”. Does no one know the meaning of the word “standard” ? A FIPS code is a useful thing. For counties it is a FIVE digit code. Got that, five?

Large numbers of people have apparently NOT gotten that. It would be immensely convenient to have FIPS codes defined the same everywhere. However, they are not. Recently, someone came by who was working with datasets some of which have state and county codes defined as numeric, so 01 = 1, 013 = 13  and she needed these merged with another file that had FIPS codes defined as string.

Just to be more annoying the length of the numeric FIPS codes was defined as 11. I don’t normally write SPSS syntax; I think SAS is preferable for data management. However, as a consultant my job isn’t to tell people what software to use but rather to help them use whatever they have chosen. So, here is one solution for the FIPS problem. FIRST change STATE and COUNTY from numeric to string, then run this code.

STRING  stfips (A13).
STRING  ctyfips (A14).
IF  (LENGTH(LTRIM(County)) = 3) ctyfips=LTRIM(county).
IF  (LENGTH(LTRIM(county)) = 2)  ctyfips=CONCAT(‘0′,LTRIM(county)).
IF  (LENGTH(LTRIM(county)) = 1)  ctyfips=CONCAT(’00’,LTRIM(county)).
STRING  fips (A5).
COMPUTE fips=CONCAT(rtrim(stfips),ltrim(ctyfips)).

Here are several problems fixed – the county and state FIPS codes had leading blanks. How annoying. LTRIM takes care of those.  The LENGTH function is used with the LTRIM function to determine if there is only 1 digit for state code. If so, a 0 is added to the front. Otherwise, the state fips code is just the existing two digits entered. Similarly, if the trimmed county fips is one digit, two zeroes are added, if it is two digits, one zero is added and if it is three digits nothing is added.

This is a really simple, common problem and I thought, using my new method of using Google to find solutions already written, I would find a snippet of code easily so I would not have to exert any effort. Unfortunately, no. I could not expect some very nice person to fill in FIPS codes by hand for 3,000 counties times eight categories. That would just be wrong. So, now she can open the 50 state files, run this code for each one and FIPS is once again standard.

My work here is done. I am heading to The Galley, known as the place to get the best steaks and best martinis in Santa Monica.

Similar Posts

One Comment

  1. I’m working on a policy report where we are gathering county level data from the American Community Survey (and relying on FIPS to get the county identifiers) and let me tell you, the code below provided on the page “Fixing FIPS – SPSS solves today’s annoyance” just saved an overextended research assistant a whole forty hour work week of doing data entry to get the FIPS codes usable. THANK YOU THANK YOU THANK YOU!

    STRING stfips (A13).
    STRING ctyfips (A14).
    IF (LENGTH(LTRIM(County)) = 3) ctyfips=LTRIM(county).
    IF (LENGTH(LTRIM(county)) = 2) ctyfips=CONCAT(‘0′,LTRIM(county)).
    IF (LENGTH(LTRIM(county)) = 1) ctyfips=CONCAT(’00’,LTRIM(county)).
    STRING fips (A5).
    COMPUTE fips=CONCAT(rtrim(stfips),ltrim(ctyfips)).

Leave a Reply

Your email address will not be published. Required fields are marked *