# Re: st: Calculate distance between two zip codes

 From Todd Wagner To statalist@hsphsun2.harvard.edu Subject Re: st: Calculate distance between two zip codes Date Wed, 07 May 2008 11:43:43 -0700

April,

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
http://www.herc.research.va.gov/resources/faq_h02.asp.

***************************************************
** 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;

dlon=long2-long1;
dlat=lati2-lati1;
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,

Todd

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:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/
```*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```