# RE: st: calculating longitudinal-latitudinal distances

 From "Nick Cox" To Subject RE: st: calculating longitudinal-latitudinal distances Date Thu, 20 Apr 2006 21:28:44 +0100

```A detail that caught my eye:

The loop starts

forv i=1/`=_N' {
su lat in `i', meanonly
scalar plat=r(mean)
su lon in `i', meanonly
scalar plon=r(mean)

This seems needlessly indirect.
The mean of a single value is
just that value, so

forv i=1/`=_N' {
scalar plat = lat[`i']
scalar plon = lon[`i']

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

Austin Nichols

> Assuming you have a dataset of legal providers, you don't need to
> treat people differently depending on what type of state they live in,
> since the nearest provider for some people cannot lie inside their
> state, while it may for others, but the nearest provider *can* lie
> outside the state for either type of person.  Assume you've got N obs,
> one per person, with lat/lon in your primary dataset people.dta, and M
> providers with lat/lon in your provider data prov.dta. Then try this
> approach on for size (assumes you may want to know something about the
> nearest provider other than distance to it, e.g. what state it's in,
> or costs of services):
>
> use people
> gen provid=.
> gen mindist=.
> sort id
> forv i=1/`=_N' {
>  su lat in `i', meanonly
>  scalar plat=r(mean)
>  su lon in `i', meanonly
>  scalar plon=r(mean)
>  preserve
>  use prov, clear
>  vincenty lat lon plat plon, gen(dist)
>  su dist, meanonly
>  keep if dist==r(min)
>  * What do you do if multiple providers are all closest?
>  * I pick one at random.
>  * You could instead: scalar n=_N and save IDs
>  *  in a loop, then expand n after the restore
>  scalar cprovid=provid
>  scalar mdist=dist
>  restore
>  replace provid=cprovid in `i'
>  replace mindist=mdist in `i'
> }
> sort provid
> merge provid using prov, uniqusing
>
> You can get vincenty.ado via the command
> net from http://www-personal.umich.edu/~nicholsa/stata
> or use your own method.
>
> All of the above is untested code--use at own risk...
>

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