# RE: st: exact command for distance ?

 From Roy Wada To Subject RE: st: exact command for distance ? Date Thu, 10 Sep 2009 16:13:36 -0700

```> Now, I need to write a small do file explaining to STATA to calculate the
> distance using the following Great Circle Formula:
>
> I thought a loop would be the best, but I have small doubts about how
> to write the command on stata. In particular, I don't exactly know how
> to tell STATA to use 2 different datasets ...
>
> using farms
> forvalues j =1/800
> cross using waterbod
> 3963*acos(sin(y/57.2958)*sin(y2/57.2958)+cos(y/57.2958)*cos(y2/57.2958)*cos((x2/57.2958)-(x/57.2958))),> name near_dist
> sort near_dist
> keep in 1
> label variable dist "nearest waterbody"
> list farm_ID farm_X farm_Y near_dist
> save ?????

If you are asking for general advice on matching, those based on -merge-
or -cross- will run very slowly, although not as slowly as one-by-one
comparison within loops whose running time will increase exponentially
with data size.

If you are asking for a program that will do this in a reasonable amount
of time, then -distmatch- is available from ssc.

After appending your dataset, the syntax would be:

distmatch, id(id) near(1) long(long) lat(lat)

where id is a unique identifier that should be left missing (replace id=.)
if the observation is to be a non-donor, which in your case would be the farm
for which the water bodies would be matched.

-distmatch- implements haversine formula, which is almost exactly the same
as the Great Circle Formula and is more accurate when the distance is less
than 1 mile.

-distmatch- was first discussed here:

http://www.stata.com/statalist/archive/2009-08/msg01536.html

Roy

_________________________________________________________________
Get back to school stuff for them and cashback for you.
http://www.bing.com/cashback?form=MSHYCB&publ=WLHMTAG&crea=TEXT_MSHYCB_BackToSchool_Cashback_BTSCashback_1x1
*
*   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/
```