2

I am trying to set a variable equal to state fips codes given its state abbreviation. Is there a shorter way to do this other than:

replace fips = "[fips code]" if other_variable=="[state_abbrev]"

Which I currently have 50 lines of. I would like to create a loop, but given that I have two changing values, I don't know how to avoid looping through every permutation.

2
  • Nick's solution is what I believe Stata wants you to do, and Ricardo gave an example. Avoiding looping is a good idea.
    – Fr.
    Mar 3, 2013 at 11:53
  • 1
    I guess the original poster was puzzled about looping through two lists simultaneously. There is a general discussion of that within stata-journal.com/sjpdf.html?articlenum=pr0009 (see Section 3). The bottom line is that you need to spell out the equivalences AL <-> Alabama, etc. somehow. (A quite different comment is that the original poster assumes it is obvious that "we" are all in the United States and know about 50 states and fips codes. It is not difficult to guess his context, but this is an international forum!)
    – Nick Cox
    Mar 5, 2013 at 10:56

2 Answers 2

2

Here is an example of the strategy covered in the FAQ.

1) Create a dataset containing two variables: the state name and the associated fips code. To make this slightly more flexible, I include common semi-abbreviations for the state name. In the future, you could add a third variable that includes the two-letter state abbreviation.

clear
input fips str20 state
1 "alabama"
2 "alaska"
4 "arizona"
5 "arkansas"
6 "california"
8 "colorado"
9 "connecticut"
10 "delaware"
11 "district of columbia"
12 "florida"
13 "georgia"
15 "hawaii"
16 "idaho"
17 "illinois"
18 "indiana"
19 "iowa"
20 "kansas"
21 "kentucky"
22 "louisiana"
23 "maine"
24 "maryland"
25 "massachusetts"
26 "michigan"
27 "minnesota"
28 "mississippi"
29 "missouri"
30 "montana"
31 "nebraska"
32 "nevada"
33 "new hampshire"
34 "new jersey"
35 "new mexico"
36 "new york"
37 "north carolina"
37 "n. carolina"
38 "north dakota"
38 "n. dakota"
39 "ohio"
40 "oklahoma"
41 "oregon"
42 "pennsylvania"
44 "rhode island"
45 "south carolina"
45 "s. carolina"
46 "south dakota"
46 "s. dakota"
47 "tennessee"
48 "texas"
49 "utah"
50 "vermont"
51 "virginia"
53 "washington"
54 "west virginia"
54 "w. virginia"
55 "wisconsin"
56 "wyoming"
72 "puerto rico"
end

save statefips, replace

2) Load your primary dataset that holds a variable with state names and perform a many-to-one merge using statefips.dta.

sysuse census, clear

// Convert the state names to lowercase to ensure
// consistency with the statefips dataset
replace state = lower(state)

merge m:1 state using statefips.dta

drop if _merge == 2
drop _merge

If you wanted to preserve the case of the state names in your master data set, you could simply generate a temporary variable and use that for the merge, i.e.

gen statelower = lower(state)
merge m:1 statelower using statefips.dta

Also, once you've created the statefips.dta data set, there's no need to recreate it every time you want to perform a merge. You could simply bundle it along with your project's files and use it when necessary. If you find you want to add two-letter state abbreviations or make some other change, then it's practically instantaneous to recreate it.

2
  • 1
    You can specify which part(s) of the merge to keep with the keep() option. See h merge.
    – Fr.
    Mar 3, 2013 at 11:51
  • 1
    @Fr. Right, like this: merge m:1 state using statefips.dta, keep(3) Mar 3, 2013 at 14:13
2

No obvious shortcut, but in Stata

 . search merge, faq

to find a relevant FAQ by Kit Baum.

1
  • I believe Stata wants the user to do it that way.
    – Fr.
    Mar 3, 2013 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.