# Re: st: Calculate distance between two zip codes

 From "Austin Nichols" To statalist@hsphsun2.harvard.edu Subject Re: st: Calculate distance between two zip codes Date Wed, 7 May 2008 21:43:01 -0400

```Nick, April Knill, et al.:

The solution I posted:
http://www.stata.com/statalist/archive/2008-05/msg00285.html
calculates distance using three methods, including the Haversine
method Todd Wagner implicitly endorses.

But the code posted is not accurate enough for many purposes that use
actual locations on a human scale, e.g. calculating which is the
nearest hospital as the crow flies (all of these solutions are for
crows only, not those of us who use conveyances).

Suppose you want to calculate the distance between two airplanes in

ssc install vincenty, replace
vincenty 61.18 -150 61.21 -149.84
return list

gives you three calculations--the first two assume the Earth is a
perfect sphere, which it is not, and the third that is an ellipsoid,
which is closer to the truth.
r(loc) : "5.712498502835709"
r(hav) : "5.712498503094725"
r(vin) : "5.735680698573105"

If you do the same calculation using Todd Wagner's code:

loc a= (sin(_pi/360*.03))^2+cos(_pi/180*61.18)*cos(_pi/180*61.21)*(sin(_pi/360*.16))^2
di " distance=" 3957*2*atan2(sqrt(`a'),sqrt(1-`a'))

you get in the ballpark, but not the right answer.  The difference
between his Haversine calc and mine is the radius of the Earth--I
assume that when the Earth is a perfect sphere it has a radius of
3956.5438 miles.

Anyway, the important point, I think, is that an error of more than
two tenths of a mile (nearly .4km) can change the outcome of
subsequent calculations (e.g. which is the nearest hospital) since the
sizes of such errors are not the same in all directions.

Want to look up my hypothetical airplanes?  Google them:

On Wed, May 7, 2008 at 3:02 PM, Nick Cox <n.j.cox@durham.ac.uk> wrote:
> 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/
```