BookmarkSubscribeRSS Feed

How Much Farther? Calculating Distance in SAS

Started 2 weeks ago by
Modified 2 weeks ago by
Views 93

Spatial analysis is an essential part of modern analytics. One common task is calculating distances between sets of points - for example, the distance between a store and a customer, a warehouse and a delivery location, or between two cities. In this post, I'll discuss how SAS makes it easy to work with geographic coordinates and compute these distances efficiently.

 

 

The GEODIST Function

 

The GEODIST function returns the distance between two sets of latitude and longitude coordinates. It takes four arguments: the latitude and longitude of the first point, and the latitude and longitude of the second point. You can also specify whether you want the results returned in miles or kilometers, and whether the input data is expressed in degrees or radians.

 

GEODIST(latitude-1, longitude-1,   

              

  latitude-2, longitude-2          

      

  <,'options'>)

 

Calculating the distance between two points has many possible applications. You could calculate the distance between a data set consisting of coordinate points and a single fixed set of coordinates. For example, imagine calculating the distance between a data set of customer coordinates and the location of a single store. You could also calculate this distance between two sets of coordinates in the same row – for example, an origin and a destination.

treiman_dist1.png

Select any image to see a larger version.
Mobile users: To view the images, select the "Full" version at the bottom of the page.

 

 

Putting it Together

 

Let’s look at an example. We’ll start with by building a simple example data set. Each row contains an origin city, a destination city, and their respective latitude and longitude coordinates:

 

data trips;
    infile datalines delimiter=',';
    input TripID $ OriginCity :$12. DestCity :$12. OriginLat OriginLon DestLat DestLon;
    datalines;
T001,New York,Los Angeles,40.7128,-74.0060,34.0522,-118.2437
T002,Chicago,Miami,41.8781,-87.6298,25.7617,-80.1918
T003,Houston,New York,29.7604,-95.3698,40.7128,-74.0060
T004,Los Angeles,Chicago,34.0522,-118.2437,41.8781,-87.6298
;
run;

 

 

Calculating Distance

 

Since each row in the data set has both origin and destination coordinates, we can use the GEODIST function to calculate the distance between them. This can be done in a DATA step:

 

data trips_distance;
    set trips;
    Distance_miles = geodist(OriginLat, OriginLon, DestLat, DestLon, 'M');
run;

 

Here, the M option specifies that the results should be returned in miles. This DATA step will create a new data set containing the Distance_miles column, with the calculated distance between each origin and destination point.

 

We can view the results:

 

proc print data=trips_distance noobs;
    var TripID OriginCity DestCity Distance_miles;
    title "Trips with Calculated Distances (miles)";
run;

 

treiman_dist2.png

 

 

Filtering Results

 

Once we’ve performed the distance calculation, the Distance_miles column can be used to identify particularly long (or short) origin-destination pairs. For example, we can use a simple PROC SQL query to identify all the trip that exceeded 1,500 miles:

 

proc sql;
    select TripID, OriginCity, DestCity, Distance_miles
    from trips_distance
    where Distance_miles > 1500;
quit;

 

treiman_dist3.png

 

 

Conclusion

 

Distance measurements are fundamental in understanding spatial relationships. Row-wise distance calculation is easy to accomplish in SAS using the GEODIST function. By combining SAS data steps, SQL, and the GEODIST function, you can build a powerful workflow for analyzing spatial relationships between points.

 

 

Tips and Tricks

 

  • The GEODIST function calculates geodetic distance between two points. It does calculate network or route distance.
  • Input coordinates can be specified in either degrees or radians. Use the R option if using radians.

 

 

Find more articles from SAS Global Enablement and Learning here.

Contributors
Version history
Last update:
2 weeks ago
Updated by:

Viya Copilot Motion Graphic.gifViya Copilot Motion Graphic

Ready to see what SAS Viya Copilot can do?

Visit the Tips & Tricks page for setup guidance, demos, and practical examples that show how Copilot supports your workflows.

Get Started →

SAS AI and Machine Learning Courses

The rapid growth of AI technologies is driving an AI skills gap and demand for AI talent. Ready to grow your AI literacy? SAS offers free ways to get started for beginners, business leaders, and analytics professionals of all skill levels. Your future self will thank you.

Get started

Article Tags