st: Distance from home to hospital [originally no subject]

 From David Kantor <[email protected]> To [email protected] Subject st: Distance from home to hospital [originally no subject] Date Wed, 03 Jan 2007 22:35:27 -0500

In response to Kelly Richardson's question about the distance between home and hospital:

The structure of this situation suggests a -cross- operation on the two datasets (persons and hospitals) -- at least in theory.
This would yield a _very_ long dataset. But this is impractical
You might want to loop through one person at a time, joining hospital data; then select the nearest (or, say the nearest 5 hospitals); then somehow output just these (maybe using -post-).

There may be a way to do this in mata; I am not versed in mata (yet).

In plain Stata, I might do the following (a mixture of real code and psuedocode):

des using personfile
local N = r(N) // num observations

forvalues j = 1 / `N' {
use personfile in `j'
cross using hospitalfile
--compute the distance; call it dist --
sort dist
keep in 1/5 // or however may closest hospitals you want
-- somehow save this info --
}

For "compute the distance", I leave that to you, as my present focus is the data structure. Austin Nichols had some advice on that matter.

For "somehow save this info", you can...
-post- it,
or save it as a tempfile (one of many tempfiles)-- and later gather the pieces.

For the latter approach, you might do (within the loop):
keep [some appropriate set of variables]
tempfile t`j'
save `t`j''

Then in a separate loop:
forvalues j = 1 / `N' {
if `j' ==1 {
use `t`j''
}
else {
append using `t`j''
}

----

I hope this helps; good luck.
--David

At 09:35 PM 1/3/2007, Austin Nichols wrote:

```Kelly--
This comes up just often enough to warrant an FAQ, but I don't know if
there is one.  There is a program that _almost_ does what you want
called -nearest- written by Nick Cox on 10 January 2003 and available
by typing -ssc install nearest- but it does not calculate distance on
a spherical or ellipsoidal model of the Earth.  I assume when you say
x,y you mean you have signed decimal degrees lat and lon.  In that
case, you can calculate distance using the -vincenty- program
vincenty lat1 lon1 lat2 lon2, g(d1) h(d2)
to generate distance variables.

Don't have the -vincenty- program?  Here it is:
net from http://www-personal.umich.edu/~nicholsa/stata
net install vincenty

If you want to calculate distance to each hospital (or each VA and
non-VA hosp), you can merge the hospital data onto the patient data,
and do the calculations inside a loop that executes once for each
you will likely get more specific advice...

On 1/3/07, Richardson, Kelly K. <[email protected]> wrote:
```
```Dear Statalisters,
I would like to calculate the distance between a patient's home and the
nearest hospital to their home. I have the x,y coordinates for the
patient's home and the x,y coordinates for every hospital in the
country. How do I get the distance to the nearest hospital? I looked at
the globdist command but I can't see how to enter coordinate variables
for both locations. For example, say I have 1000 patients across the
country and 5000 hospitals. How do I get one distance for each patient
that represents the distance to the nearest hospital? I'm not clear on
how my data should be constructed or which command to use. Right now the
data are in separate files, one for patients (n=1000) and one for
hospitals (n=5000).  [...]
```
```*
*   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/
```