I'm trying to find the distance between two lat/long points in Stata. I have 15 lat/long pairs of coordinates in a wide format, and I want to find the distance between (lat1, long1), (lat2, long2) and (lat2, long2), (lat3, long3), etc. Below is an example of two points.
lat1 lon1 lat2 lon2
39.9522613 -75.1949641 39.9482853 -75.1962355
I found the "geodist" function to be useful, so I'd like to use it to loop through everything. I tried the following, but it did not work. Any suggestions?
I also previously had my data in LONG format, with each lat/long coordinate on a separate row, and I tried the following with no luck.
lat1 lon1 lat2 lon2
39.9522613 -75.1949641 39.9482853 -75.1962355
I found the "geodist" function to be useful, so I'd like to use it to loop through everything. I tried the following, but it did not work. Any suggestions?
Code:
forval i = 1/15 { geodist lat`i' lon`i' lat(`i'+1) lon(`i'+1), gen(km_`i') } Was expecting a number or a numeric scalar instead of -lat(1+1)- r(198);
Code:
geodist latitude longitude latitude[_n-1] longitude[_n-1], gen(km)
Comment