Statalist


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

RE: st: Calculate distance between two zip codes


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: Calculate distance between two zip codes
Date   Wed, 7 May 2008 20:02:32 +0100

I have never used SAS, but I can help a little. 

As commented, atan(1)/45 is a conversion factor to go from degrees to
radians. In Stata terms that is _pi / 180. Whatever the language being
used, that need only be calculated once; in Stata you do not need to run
atan() for that purpose. 

I don't know if these formulae imply tunnelling from one place to
another, but I haven't read the documentation either. 

Nick 
n.j.cox@durham.ac.uk 

Todd Wagner
===========

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/

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



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