

Let’s say we wish to find the distance of New York from other cities in the US.Ĭalculating the Distance of New York From Other Cities The double format is more precise and accurate than float.

We type the following command with the first two arguments being the latitude (40.697132) and longitude (-73.931351), respectively, of New York. The latitude and longitude of other cities are stored in ‘’latitudes’ and ‘longitudes’ respectively. The distance between each of these cities from New York will be stored in a new variable generated called ‘km’. The distances stored in the new ‘km’ variable are air distances as opposed to road distances. To compute the above distances in miles, we just add an option mile: geodist 40.697132 -73.931351 latitude longitude, gen(mile) mile This is because this command calculates the geodetic distance, which is the length of the shortest curve between two points on the earth’s spherical surface. This generates a variable ‘mile’ with the distance of each city from New York stored in miles. Haversine and Vincenty (1975) Formulae for Distance:īy default, Stata uses the Vincenty (1975) formula to calculate the distance through geodist. If we require the Haversine formula to be used, we add an option of sphere. Using geodist with Longitudes and Latitudes Stored In a Variable The Vincenty (1975) and Haversine formulas produce fairly similar measures of distance.

Let’s first generate variables that hold longitude and latitude values for New York: generate baselat = 40.697132 generate baselong = -73.931351 If our longitudinal and latitudinal values were stored in a variable, and we did not want to write entire numbers in our commands manually, we simply use the respective variable names. This generates two variables ‘baselat’ and ‘baselong’ that are formatted as float.
