C# Dictionary of US State Abbreviation to Names using SAS

I need C# code to convert United States state postal abbreviations (like AL) to full state names (like Alabama), but I’m afraid of making a typo retyping the abbreviation list from the USPS—and that just seems tedious. However, it’s easy generate the C# code as a dictionary data structure using simple SAS code and SAS functions (i.e., metaprogramming).

One key to making this easy is that SAS has a function which converts a numeric state FIPS code to both the state abbreviation and state name. For example, fipnamel(1) = Alabama and fipstate(1) = AL. After that, it’s mainly a matter of looping.

data states;
	do i = 1 to 56;
		name = fipnamel(i);
		abbr = fipstate(i);
		csharp = cats( 'states.Add("', abbr, '", "', name,'");');
		if name ne 'Invalid Code' and abbr ne '--' then do;
			output;
			put csharp;
		end;
	end;
run;

Now scraping the SAS output log, I make my C# function.

public string stateAbbreviationExpand(string abbr)
    {
        Dictionary<string, string> states = new Dictionary<string, string>();

        states.Add("AL", "Alabama");
        states.Add("AK", "Alaska");
        states.Add("AZ", "Arizona");
        states.Add("AR", "Arkansas");
        states.Add("CA", "California");
        states.Add("CO", "Colorado");
        states.Add("CT", "Connecticut");
        states.Add("DE", "Delaware");
        states.Add("DC", "District of Columbia");
        states.Add("FL", "Florida");
        states.Add("GA", "Georgia");
        states.Add("HI", "Hawaii");
        states.Add("ID", "Idaho");
        states.Add("IL", "Illinois");
        states.Add("IN", "Indiana");
        states.Add("IA", "Iowa");
        states.Add("KS", "Kansas");
        states.Add("KY", "Kentucky");
        states.Add("LA", "Louisiana");
        states.Add("ME", "Maine");
        states.Add("MD", "Maryland");
        states.Add("MA", "Massachusetts");
        states.Add("MI", "Michigan");
        states.Add("MN", "Minnesota");
        states.Add("MS", "Mississippi");
        states.Add("MO", "Missouri");
        states.Add("MT", "Montana");
        states.Add("NE", "Nebraska");
        states.Add("NV", "Nevada");
        states.Add("NH", "New Hampshire");
        states.Add("NJ", "New Jersey");
        states.Add("NM", "New Mexico");
        states.Add("NY", "New York");
        states.Add("NC", "North Carolina");
        states.Add("ND", "North Dakota");
        states.Add("OH", "Ohio");
        states.Add("OK", "Oklahoma");
        states.Add("OR", "Oregon");
        states.Add("PA", "Pennsylvania");
        states.Add("RI", "Rhode Island");
        states.Add("SC", "South Carolina");
        states.Add("SD", "South Dakota");
        states.Add("TN", "Tennessee");
        states.Add("TX", "Texas");
        states.Add("UT", "Utah");
        states.Add("VT", "Vermont");
        states.Add("VA", "Virginia");
        states.Add("WA", "Washington");
        states.Add("WV", "West Virginia");
        states.Add("WI", "Wisconsin");
        states.Add("WY", "Wyoming");
        if (states.ContainsKey(abbr))
            return (states[abbr]);
        /* error handler is to return an empty string rather than throwing an exception */
        return "";      
    }
About these ads

5 thoughts on “C# Dictionary of US State Abbreviation to Names using SAS

  1. great but would be better if it included common misspellings and alternate abbrevs, like “newyork” or “calif” and the 12 ways people might spell mississippi. it would also be amazing if it was bi-directional, so that length=2 expands, length>2 abbreviates

  2. Yikes! That method rebuilds the entire Dictionary on each lookup. Hardly efficient at all. The Dictionary should be built once (or as-needed if more states pop into existence) either statically or in an initialization method and then queried (“expanded”) afterward by the caller.

    • Yes, in some situations this would waste CPU time, but in my situation, it wasn’t called enough to make a noticeable difference. If anyone has a better version, please post a comment using sourcecode markup.

      • Here’s a stab at it:

                private static readonly IDictionary<string, string> states = new Dictionary<string, string>
                {
                    { "AL", "Alabama" },
                    { "AK", "Alaska" },
                    { "AZ", "Arizona" },
                    { "AR", "Arkansas" },
                    { "CA", "California" },
                    { "CO", "Colorado" },
                    { "CT", "Connecticut" },
                    { "DE", "Delaware" },
                    { "DC", "District of Columbia" },
                    { "FL", "Florida" },
                    { "GA", "Georgia" },
                    { "HI", "Hawaii" },
                    { "ID", "Idaho" },
                    { "IL", "Illinois" },
                    { "IN", "Indiana" },
                    { "IA", "Iowa" },
                    { "KS", "Kansas" },
                    { "KY", "Kentucky" },
                    { "LA", "Louisiana" },
                    { "ME", "Maine" },
                    { "MD", "Maryland" },
                    { "MA", "Massachusetts" },
                    { "MI", "Michigan" },
                    { "MN", "Minnesota" },
                    { "MS", "Mississippi" },
                    { "MO", "Missouri" },
                    { "MT", "Montana" },
                    { "NE", "Nebraska" },
                    { "NV", "Nevada" },
                    { "NH", "New Hampshire" },
                    { "NJ", "New Jersey" },
                    { "NM", "New Mexico" },
                    { "NY", "New York" },
                    { "NC", "North Carolina" },
                    { "ND", "North Dakota" },
                    { "OH", "Ohio" },
                    { "OK", "Oklahoma" },
                    { "OR", "Oregon" },
                    { "PA", "Pennsylvania" },
                    { "RI", "Rhode Island" },
                    { "SC", "South Carolina" },
                    { "SD", "South Dakota" },
                    { "TN", "Tennessee" },
                    { "TX", "Texas" },
                    { "UT", "Utah" },
                    { "VT", "Vermont" },
                    { "VA", "Virginia" },
                    { "WA", "Washington" },
                    { "WV", "West Virginia" },
                    { "WI", "Wisconsin" },
                    { "WY", "Wyoming" }
                };
        
                public static string StateAbbreviationExpand(string abbr)
                {
                    string state;
        
                    /* error handler is to return an empty string rather than throwing an exception */
                    return states.TryGetValue(abbr, out state) ? state : string.Empty;
                }
        

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s