As the first step in the decommissioning of sasCommunity.org the site has been converted to read-only mode.

Here are some tips for How to share your SAS knowledge with your professional network.

# Calculating Distance Between Two Zip Codes

## Calculating Distance Between Two Zip Codes

Below is a one-pass algorithm for this problem.

Note-1: the point of this program is to show the use of temporary arrays as look-up tables.

>>>---> NOTE-2: <---<<<
I do not know the units of the variable **Distance**.

- (
*The SAS Knowledgebase Note about this problem states the distance is in***Miles**. Presumably, to obtain the distance in kilometres then the constant 3949.99 needs to be scaled by the proportion of kilometres per mile, or about 1.6 km/mi. The ZIPCITYDISTANCE function in SAS 9.2 may be easier to use.)

These lat/lon values are include in the zipcode table located in SASHELP in any standard SAS installation.

http://support.sas.com/techsup/unotes/SN/005/005325.html

Here is the meat of the article:

Assume the longitude and latitude for the first ZIP code are LONG1 and
LAT1, respectively, and the longitude and latitude for the second ZIP
code are LONG2 and LAT2, respectively. The **Great Circle Distance**
formula is as follows:

Dist = 3949.99 * arcos( sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(long1 - long2));

*Note: The Great Circle Distance assumes the earth is a sphere, rather than an ellipsoid. Angles are in radians, rather than degrees.*

*compute-distances-between-two-zipcodes.sas; /*proc sql;describe table sashelp.zipcode;quit;*/ Data TwoZips; Zip1 = 30341;Zip2 = 30342;output; Zip1 = 30343;Zip2 = 30345;output; Zip1 = 30348;Zip2 = 30311;output; Zip1 = 30341;Zip2 = 20525;output; DATA Distances (keep = Zip1 Zip2 Dist); attrib Testing length = 4; retain Testing 1; *allocate look-up table; array Latt(99999) _temporary_ (99999*0); array Long(99999) _temporary_ (99999*0); *load look-up table; do until(EndoLoad); set sashelp.zipcode end = EndoLoad; Latt(Zip) = Y; Long(Zip) = X; end; if testing then putlog Latt(30341)= Long(30341)=; do until(EndoFile); set work.TwoZips end = EndoFile; Dist = 3949.99 * arcos(sin( latt(zip1)/57.2958) * sin( latt(zip2)/57.2958) + cos( latt(zip1)/57.2958) * cos( latt(zip2)/57.2958) * cos( long(zip1)/57.2958 - long(zip2)/57.2958 ) ); output; end; stop; run; PROC Print data = Distances; run;

--macro maven == the radical programmer 11:32, 14 September 2007 (EDT)