BookmarkSubscribeRSS Feed
chimei0403
Obsidian | Level 7

Hello SAS experts,

I encountered 3 quick questions regarding distance/travel time  between two zip codes.

 

1. zipcitydistance is helpful to calculate the distances between two groups easily. However, I didn't find the code that could give me the travel time.

Is there a convenient way to calculate the travel time between two zip codes?

 

2. Is there a limit sample to calculate the distance of two zip codes? 

I got some error like below when running big datasets.

WARNING: Limit set by ERRORS= option reached. Further errors of this type will not be printed.
NOTE: The DATA step has been abnormally terminated.
NOTE: Mathematical operations could not be performed at the following places. The results of the operations have been set to missing values.

 

3.  Why the results of the distance is different than I put the zip code in other website individually? 

 

data have;
    input HQ_ZIP AFF_ZIP;
    datalines;
10001 90001
98101 32789
;
run;

data want;
    set have;
    distance = zipcitydistance(HQ_ZIP, AFF_ZIP);
run;

chimei0403_0-1663175072049.png 

You can see the distance between zip 10001 and zip 9001 is 2454.1.

But if I put those zip codes to  this website, https://www.zipcodesoft.com/distance-between-zip-codes

I got something like close to 2448.

chimei0403_1-1663175321741.png

 

I don't know which to follow.

Is there a simple way to calculate road distance of the two zip codes?

Many thanks!

 

2 REPLIES 2
mkeintz
PROC Star

@chimei0403 wrote:

Hello SAS experts,

I encountered 3 quick questions regarding distance/travel time  between two zip codes.

 

1. zipcitydistance is helpful to calculate the distances between two groups easily. However, I didn't find the code that could give me the travel time.

Is there a convenient way to calculate the travel time between two zip codes?

 

2. Is there a limit sample to calculate the distance of two zip codes? 

I got some error like below when running big datasets.

WARNING: Limit set by ERRORS= option reached. Further errors of this type will not be printed.
NOTE: The DATA step has been abnormally terminated.
NOTE: Mathematical operations could not be performed at the following places. The results of the operations have been set to missing values.

 

3.  Why the results of the distance is different than I put the zip code in other website individually? 

 

  1. Travel time would require data on connections via the mode of travel (e.g. a map of all roads).  This is most certainly NOT in the sas library.  The zipcitydistance function uses the sashelp.zipcode data, which will provide zip code centroids.  That's good enough for distances as the bird flies, but not distance (and therefore travel time) via a network of roads.

  2. You can probably increase the ERRORS limit, but it's more important to find out WHY you're getting errors.  Namely look at the code in the locations pointed out by the note on the log.  Show us the code, and the log it generates, to get more useful help/

  3. Distance can be different if the various sources use different zip code data.  SAS uses the zip code centroids in sashelp.zipcode.  But the others may use slightly different centroid values - or may choose to use some other two-dimensional point for each zip code.
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
ballardw
Super User

If you are getting errors from the ZIPCITYDISTANCE function it means that either one, or both, Zip codes is missing or invalid.

For this function Zip+4 codes are "invalid" because the Sashelp.zipcode data set does not have centroids for the +4 versions.

This is clear with this example:

225  data example;
226     x1= zipcitydistance('83701','83704');
227     x2= zipcitydistance('83701','83704-1069');
228  run;

NOTE: Invalid argument to function ZIPCITYDISTANCE('83701','83704-1069') at line 227 column 8.
x1=1.3 x2=. _ERROR_=1 _N_=1

If you have real data like I get then Zip codes can have too many digits, missing digits, typos reversing digits which often are invalid but not always, or characters other than digits.

 

If your errors do not include ZIPCITYDISTANCE then you need to provide the entire log from your code. Copy the text from the log, open a text box on the forum using the </> icon that appears above the message window and paste in the box. It should look like my example above.

 

Do note that the documentation for the function states:

The ZIPCITYDISTANCE function returns the geodetic distance in miles between two ZIP code locations. The centroid of each ZIP code is used in the calculation.

using this definition for Geodetic:

the shortest line that can be drawn between two points on the elipsoidal surface of the earth; a curve drawn on any given surface so that the osculating plane of the curve at every point shall contain the normal to the surface; the minimum line that can be drawn on any surface between any two points.

 

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 998 views
  • 0 likes
  • 3 in conversation