BookmarkSubscribeRSS Feed
jz
Calcite | Level 5 jz
Calcite | Level 5

I would like to calculate the distance between two columns (about 100 rows) of zipcodes (source zipcodes and destined zipcodes) in SAS university version. 

 

I first used the zipcitydistance function, but got an error message saying that "SASHELP.ZIPCODE.DATA" does not exist. I didn't know why, but when I checked the SASHELP library, it was not there.

 

Second, I downloaded manually the recent ZIPCODE.DATA from MAPSONLINE, but found that I actually couldn't save into the SASHELP path becuase no data can be added there.

 

Then I manually merge the ZIPCODE.DATA which has ZIP, x (which I assmed it was latitude)), and y (which I assumed as longitude) with my zipcode pair data. I then used the GEODIST function to calculate the distances and found that SAS calculated some of pairs but not all of them. I selected one of the calculated distances and compared it with the web site tools that calculated the distance between first zipcode and second zipcode and found that the result could not be matched to what the web tool calculations. In addition, for the pair that SAS could not calculate, I still could input on a web tool and found out the distance.

 

My questions are: (1) Does SAS university version have the ZIPCITYDISTANCE function feature? (2) Why did the GEODIST function give results on some pairs but not all? and (3) why did the SAS GEODIST results differ from those from other non-SAS tools?

 

Jeff

 

=====

here are the codes i used:

data _null_;   

  dis1 =geodist(-74.009507, 40.712808, -112.016418, 41.219800, 'M');   

  dis2 =geodist(-93.469244, 41.649151, -93.617602, 41.641599, 'M');   

  dis3 =geodist(-71.071293, 42.358664, -71.163840, 42.711749, 'M');   

put "Distance between zipcodes 10007 and 84401 is " dis1;   

put "Distance between zipcodes 50009 and 50313 is " dis2;   

put "Distance between zipcodes 02108 and 01841 is " dis3;   

run;

====

Note SAS output dis1 and dis2 as blank, and 10.2 miles for dis3.  The web tools* gave 1972 miles, 7.7 miles, and 25 miles respectively.

 

*: e.g., https://www.melissadata.com/lookups/zipdistance.asp;  or http://www.allplaces.us/dbz.cgi

 

5 REPLIES 5
Darrell_sas
SAS Employee

You have Lat and Long backwards.  X=long y=lat.

I am still checking on why SASHELP.ZIPCODE doesn't exist.

etprice
Calcite | Level 5

I'd also like to use sashelp.zipcode in the SAS university edition.  I'm interested in understanding why it isn't included the university version.

Thanks!

ballardw
Super User

Look at the documentation for GEODIST. The Latitude goes before Longitude in each pair. Note that LATITUDE has a range of +/-90.

It appears that you have the coordinates in the wrong order:

data _null_;   
  dis1 =geodist( 40.712808,-74.009507, 41.219800, -112.016418, 'M');   
  dis2 =geodist( 41.649151,-93.469244,  41.641599,-93.617602, 'M');   
  dis3 =geodist( 42.358664,-71.071293, 42.711749, -71.163840, 'M');   
  put "Distance between zipcodes 10007 and 84401 is " dis1;   
  put "Distance between zipcodes 50009 and 50313 is " dis2;   
  put "Distance between zipcodes 02108 and 01841 is " dis3;   
run;

Comes pretty close to matching the web tool values you report.

 

jz
Calcite | Level 5 jz
Calcite | Level 5

Thanks a lot. I indeed placed x and y incorrectly. After I realligned by placing in the order of (Y1, X1, Y2, X2, 'M') it worked out perfectly.  SAS GEODIST calculated all on all valid latitude and longitude values.  I checked on the first 5 records against the other web tools (which can only show the result for one pair of lat. and long. at a time) and noticed that the results are very very close (thought SAS results were consistently a bit lower (about 0.1-0.2 miles lower for distances in the 3-7 miles range). The results are reliable and appear to serve my purpose of evaluating the relative distances among the pairs of zipcodes very well.

 

This is my first time to post; I am very grateful for the quick and very useful replies from the SAS communities. 

 

fannavivian
Obsidian | Level 7

Hi this is also my first post. Contributed to geodist function~~

How accurate is HAVERSINE formula (earth radius of 3949.99 miles used to produce distance in miles)?

And how accurate is geodist to R result if running by R?

I felt SAS calculated result is a little less accurate than R. (Hope someone can explain or correct...)

 

Thanks!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 5 replies
  • 5201 views
  • 1 like
  • 5 in conversation