Subscribe to DSC Newsletter

Get state and region from zip code, with simple Perl, Python, R or C++ function

Here's an old piece of code I wrote 15 years ago. Might still be useful, as zip codes are one of the few things that have barely changed over the last decades.

The input is a variable z (zip code). Output is state attached to zip code (stored in variable s) as well as region (stored in variable r).

sub map {

local($z)=@_; # input variable (zip code)
local($s,$r); # output variables

if ($z<600) {$s=0;$r=0; }
elsif ($z<2800 && $z>=1000) {$s=25;$r=3;} # NE
elsif ($z<3000 && $z>=2800) {$s=44;$r=3;}
elsif ($z<3900 && $z>=3000) {$s=33;$r=3;}
elsif ($z<5000 && $z>=3900) {$s=23;$r=3;}
elsif ($z<6000 && $z>=5000) {$s=50;$r=3;}
elsif ($z<7000 && $z>=6000) {$s=9;$r=3;}
elsif ($z<9000 && $z>=7000) {$s=34;$r=3;}
elsif ($z<15000 && $z>=9000) {$s=36;$r=3;}
elsif ($z<19700 && $z>=15000) {$s=42;$r=3;}

elsif ($z<20000 && $z>=19700) {$s=10;$r=4;} # south
elsif ($z<20600 && $z>=20000) {$s=11;$r=4;}
elsif ($z<22000 && $z>=20600) {$s=24;$r=4;}
elsif ($z<24700 && $z>=22000) {$s=51;$r=4;}
elsif ($z<27000 && $z>=24700) {$s=54;$r=4;}
elsif ($z<29000 && $z>=27000) {$s=37;$r=4;}
elsif ($z<30000 && $z>=29000) {$s=45;$r=4;}
elsif ($z<32000 && $z>=30000) {$s=13;$r=4;}
elsif ($z<35000 && $z>=32000) {$s=12;$r=4;}
elsif ($z<37000 && $z>=35000) {$s=1;$r=4;}
elsif ($z<38600 && $z>=37000) {$s=47;$r=4;}
elsif ($z<40000 && $z>=38600) {$s=28;$r=4;}
elsif ($z<43000 && $z>=40000) {$s=21;$r=4;}
elsif ($z<73000 && $z>=71600) {$s=5;$r=4;}
elsif ($z<71500 && $z>=70000) {$s=22;$r=4;}
elsif ($z<75000 && $z>=73000) {$s=40;$r=4;}
elsif ($z<80000 && $z>=75000) {$s=48;$r=4;}

elsif ($z<45900 && $z>=43000) {$s=39;$r=5;} # midwest
elsif ($z<48000 && $z>=46000) {$s=18;$r=5;}
elsif ($z<50000 && $z>=48000) {$s=26;$r=5;}
elsif ($z<52900 && $z>=50000) {$s=19;$r=5;}
elsif ($z<55000 && $z>=53000) {$s=55;$r=5;}
elsif ($z<56800 && $z>=55000) {$s=27;$r=5;}
elsif ($z<57800 && $z>=57000) {$s=46;$r=5;}
elsif ($z<58900 && $z>=58000) {$s=38;$r=5;}
elsif ($z<63000 && $z>=60000) {$s=17;$r=5;}
elsif ($z<65900 && $z>=63000) {$s=29;$r=5;}
elsif ($z<68000 && $z>=66000) {$s=20;$r=5;}
elsif ($z<69400 && $z>=68000) {$s=31;$r=5;}

elsif ($z<86600 && $z>=85000) {$s=4;$r=2;} # west
elsif ($z<81700 && $z>=80000) {$s=8;$r=2;}
elsif ($z<60000 && $z>=59000) {$s=30;$r=2;}
elsif ($z<83200 && $z>=82000) {$s=56;$r=2;}
elsif ($z<83900 && $z>=83200) {$s=16;$r=2;}
elsif ($z<84800 && $z>=84000) {$s=49;$r=2;}
elsif ($z<88500 && $z>=87000) {$s=35;$r=2;}
elsif ($z<89900 && $z>=89000) {$s=32;$r=2;}
elsif ($z<96700 && $z>=90000) {$s=6;$r=2;}
elsif ($z<96900 && $z>=96700) {$s=15;$r=2;}
elsif ($z<99500 && $z>=98000) {$s=53;$r=2;}
elsif ($z<100000 && $z=>99500) {$s=2;$r=2;}
elsif ($z<98000 && $z>=97000) {$s=41;$r=2;}

elsif ($z<97000 && $z>=96900) {$s=-1;$r=0;}
elsif ($z<1000 && $z>=600) {$s=72;$r=0;}
else {$s=-2;$r=0;}

return($s,$r);

}

Views: 2161

Follow Us

On Data Science Central

On DataViz

On Hadoop

© 2017   AnalyticBridge.com is a subsidiary and dedicated channel of Data Science Central LLC   Powered by

Badges  |  Report an Issue  |  Terms of Service