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

From |
"Austin Nichols" <austinnichols@gmail.com> |

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 Anchorage, Alaska: 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: http://maps.google.com/maps?f=q&hl=en&geocode=&q=61.21,-149.84&ie=UTF8&ll=61.210002,-149.839997&spn=0.005983,0.023518&t=h&z=16 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/

**References**:**st: Calculate distance between two zip codes***From:*"Knill, April" <aknill@cob.fsu.edu>

**Re: st: Calculate distance between two zip codes***From:*Todd Wagner <twagner@stanford.edu>

**RE: st: Calculate distance between two zip codes***From:*"Nick Cox" <n.j.cox@durham.ac.uk>

- Prev by Date:
**Re: st: Median level** - Next by Date:
**st: how to suppress the reporting of some regressors?** - Previous by thread:
**RE: st: Calculate distance between two zip codes** - Next by thread:
**st: Reshape and file could not open** - Index(es):

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