Monday, November 21, 2016

Hack 4.2 Calculating Geodetic Distances with the ZIPCITYDISTANCE Function

SAS Programming Professionals,

Did you know that you can calculate the geodetic distance, in miles, between any two zip codes with SAS?

The aptly named ZIPCITYDISTANCE function allows you to specify both of the zip codes and then compute the distance between them.  Here is an example:

data gointoseattle;

seattle_dist = put(zipcitydistance(20850,98101),comma.);

label seattle_dist = "My Travel Distance from Rockville to Seattle";

run;

proc print noobs data=gointoseattle label;

run;

The code, above, produces the following output:

My Travel Distance
  from Rockville
  to Seattle

    2,315

Distances between Rockville and destinations in countries that do not have a US Zip code can be calculated via the GEODIST function as long as you have the longitude and the latitude of both points.  Here is an example:

data gointoseattle;

seattle_dist = put(geodist(39.085920,-77.174389,47.611330,-122.333219,"M"),comma.);

label seattle_dist = "My Travel Distance From Rockville to Seattle";

run;

proc print noobs data=gointoseattle label;

run;

This example used the GEODIST function to calculate the distance between Rockville and Seattle, and produced the same output as the previous example. 

Of course, if you are working with foreign clients, you might choose to substitute “K” (kilometers) for “M” (miles) to keep the distance in their perspective.  Miles; kilometers; what’s a few klicks between friends?

Best of luck in all your SAS endeavors!

----MMMMIIIIKKKKEEEE
(aka Michael A. Raithel)

Excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers
http://www.amazon.com/Michael-A.-Raithel/e/B001K8GG90/ref=ntt_dp_epwbk_0

No comments:

Post a Comment