This generates two variables ‘baselat’ and ‘baselong’ that are formatted as float. 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. Using geodist with Longitudes and Latitudes Stored In a Variable The Vincenty (1975) and Haversine formulas produce fairly similar measures of distance. If we require the Haversine formula to be used, we add an option of sphere. Haversine and Vincenty (1975) Formulae for Distance:īy default, Stata uses the Vincenty (1975) formula to calculate the distance through geodist. This generates a variable ‘mile’ with the distance of each city from New York stored in miles. 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. The distances stored in the new ‘km’ variable are air distances as opposed to road distances. The distance between each of these cities from New York will be stored in a new variable generated called ‘km’. The latitude and longitude of other cities are stored in ‘’latitudes’ and ‘longitudes’ respectively. We type the following command with the first two arguments being the latitude (40.697132) and longitude (-73.931351), respectively, of New York. Let’s say we wish to find the distance of New York from other cities in the US.
Calculating the Distance of New York From Other Cities The double format is more precise and accurate than float. Whenever working with distances, or the geodist command, it is a good idea to store values in variables with the double format rather than the float format. Other optional options can also be added.īy default, this command generates distances in kilometers. The option of generate()is necessary, and allows us to specify the name of the variable that will be generated to store the distance between the two locations. The latitude must always be written before the longitude. The first two arguments are the latitude and longitude of the first location, followed by the latitude and longitude of the second location. The general syntax of the command is as follows: geodist lat1 lon1 lat2 lon2, generate(new_dist_var)
Stata’s working directory can be changed through: cd “directory path” Syntax of geodist
Make sure the working directory of your Stata file is the same as the working directory of the data file. To load the dataset, we type: use "distance.dta", clear