# st: Nearest distance (spatial) and shp2dta question

 From "Sergio I Prada" To statalist@hsphsun2.harvard.edu Subject st: Nearest distance (spatial) and shp2dta question Date Fri, 3 Jul 2009 09:40:06 -0400 (EDT)

```Here another resource (which I got before from the kind people at Stata):

Havershine is a user written command that calculates the distance using
the Haversine formula, Vincenty formula, and/or the Law-of-Cosines-based
formula. Type the following to install this command:

net describe vincenty, from(http://fmwww.bc.edu/RePEc/bocode/v)
net install vincenty

Then, type -help vincenty- to view the help file. By using the hav()
option you
can calculate the distance using the Haversine formula instead of manually
entering the equation. Also, the default is to express the distances in miles
but you can change this to kilometers with the in km option.

See one example here

*********************
use testdistance, clear
set more off
sort patient
gen id = _n
qui levelsof patient, local(patients)
qui gen shortest_dist = .
qui gen matched_hos = .
foreach p of local patients {
preserve
qui keep if patient==`p'
local pat_long = pat_long[1]
local pat_lat = pat_lat[1]
local obs = id[1]
restore
vincenty `pat_lat' `pat_long' hos_lat hos_long, hav(h) replace
summarize h
qui replace shortest_dist = `r(min)' in `obs'
preserve
keep if round(h,.00001) == round(`r(min)', .00001)
local id = hospital[1]
restore
qui replace matched_hos = `id' in `obs'

}
drop id
***********************

This is calculating the straight distance from the patient to the
hospital. This is not taking into consideratin any physical land barriers,
such as mountains and rivers.

--
Department of Public Policy
University of Maryland, Baltimore County
1000 Hilltop Circle, Baltimore, MD 21250
Phone: 410-455-6527

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```