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

st: exact command for distance ?

From   Laura Platchkov <[email protected]>
To   "[email protected]" <[email protected]>
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!

*   For searches and help try:

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