[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

From |
Laura Platchkov <LMP881@bham.ac.uk> |

To |
"statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu> |

Subject |
st: exact command for distance ? |

Date |
Thu, 10 Sep 2009 20:27:52 +0100 |

Dear Statalist users, I was just wondering if someone would have an advice as to how to write a small program on STATA to compute distances between 2 datasets. I have 2 datasets (.dta files). The first, called farms.dta, contains 800 observations that correspond each of them to a farm,with, among others, the 3 variables: farm_ID, farm_X (longitude) and farm_Y (latitude). The second called waterbodies.dta contains information about the locations of the centroids of 135 waterbodies, more precisely 3 variables: waterbody_ID, wat_X, wat_Y. I want to calculate the distance of each of the farm to the nearest waterbody, in kilometers. Now, I need to write a small do file explaining to STATA to calculate the distance using the following Great Circle Formula: 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))), where x and y are the coordinates of the farms, and x2 and y2 are the coordinates of the bodies of water.... ...for each observation to each of the waterbodies, but I only need the smallest distance. 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 at the keep in 1same time and how to tell STATA to give me only the smallest distance. I want STATA to simply add the results as an additional variable in the first dataset (or create a 3d dataset with the farm_ID, farm_X, farm_Y and nearest distance if its easier). My idea is to do a write in the do-file, something like this: 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 ????? I guess there are some mistakes... Does anybody has perhaps a suggestion as how to improve the command? Thanks a lot! Laura * * 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/

**Follow-Ups**:**RE: st: exact command for distance ?***From:*Roy Wada <roywada@hotmail.com>

- Prev by Date:
**Re: st: L&F's fitstat command, contradictory results** - Next by Date:
**st: latex sidewaystable with estout?** - Previous by thread:
**st: L&F's fitstat command, contradictory results** - Next by thread:
**RE: st: exact command for distance ?** - Index(es):

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