05-04-2016 12:10 AM
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?
here are the codes i used:
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;
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.
05-04-2016 12:24 PM
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.
05-04-2016 12:51 PM
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.
12-13-2016 05:26 PM
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...)