[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Calculate distance between two zip codes

From   Todd Wagner <>
Subject   Re: st: Calculate distance between two zip codes
Date   Wed, 07 May 2008 11:43:43 -0700


One challenge is getting the latitude/longitude data. It sounds like you have that for zip codes-- I'm assuming you are using latitude/longitude for the population centroids for each zip code.

With that in mind, here is SAS code to do the rest. You should be able to convert this to Stata easily enough. My colleague has done a lot of this and the documentation is at

** Longitude and latitude values are in degrees. **
** First convert degrees to radians: **

long1 = atan(1)/45 * lon1;
lati1 = atan(1)/45 * lat1;
long2 = atan(1)/45 * lon2;
lati2 = atan(1)/45 * lat2;

a=(sin(dlat/2))**2 + cos(lati1) * cos(lati2) * (sin(dlon/2))**2;
c= 2 * atan2(sqrt(a),sqrt(1-a));
distance=3957 * c;

Good luck,


At 05:09 AM 5/7/2008, you wrote:

Good afternoon Statalisters,

Does anyone have code that calculates the distance (miles or km is fine) between two locations using zip code? I have data on zip codes which includes city, county, state, and latitude/longitude coordinates. I have found code online that uses the longitude/latitude coordinates but it is not Stata code and I have no knowledge on how to convert or make it usable within Stata. Other than that, there are plenty of websites that allow one to put in two zip codes and will calculate the distance but I'm looking for code that will do it for me. Thanks in advance!

Best Regards,

April Knill

* For searches and help try:
*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index