## Distance calculation

Occasional Contributor
Posts: 9

# Distance calculation

I have the below code. What if I have the data for lan1, lon1 in one spread sheet with 3000 rows of addresses and lat2, lon2 from another sheet with 300 rows of data?

%let rad = constant('pi') / 180;  /*degrees to radians*/
%let r_km = 6371;                 /*radius of the earth in km (3959 miles)*/

proc fcmp outlib = work.funcs.haversine;
function haversine(lat1, long1, lat2, long2);  /*function name*/
/*function definition*/
dist = &r_km * 2 * arsin(min(1, sqrt(sin((lat2 - lat1) * &rad / 2)**2 +
cos(lat1 * &rad) * cos(lat2 * &rad) * sin((long2 - long1) * &rad / 2)**2)));
/*asin should be equiv to atan2*/
difflat = lat2 - lat1;
difflong = long2 - long1;
arg = sin(difflat / 2 * &rad)**2 + cos(lat1 * &rad) * cos(lat2 * &rad) * sin(difflong / 2 * &rad)**2;
dist = 2 * &r_km * atan2(sqrt(arg), sqrt(1 - arg));
return(dist);  /*what to return*/
endsub;
run;
options cmplib = work.funcs; /*needed to access the function*/

PROC Star
Posts: 254

## Re: Distance calculation

Posted in reply to mauri0623

If I understand you correctly, you are writing a function for calculating distances on a sphere (the Earth). SAS already has a function fot that, it is called GEODIST.

Super User
Posts: 13,365

## Re: Distance calculation

Posted in reply to mauri0623

@mauri0623 wrote:

I have the below code. What if I have the data for lan1, lon1 in one spread sheet with 3000 rows of addresses and lat2, lon2 from another sheet with 300 rows of data?

Generally SAS does nothing inside "spreadsheets". You can read the data into SAS as two separate data sets and combine the records into a single data set. Then do the calculation. How you combine the data to calculate the distances to make sense will take more details.

The function GEODIST as @s_lassen said will work assuming your input data values are numeric. The function will allow you specify whether you want the results in Miles or Kilometers and whether the values are Degrees or Radians (both points have to have the same measurement unit though).

Occasional Contributor
Posts: 9

## Re: Distance calculation

May be I am not explaining the question correctly. I demo an example hoping it can shed some lights. I have no problem converting the two spread sheets into SAS. My issue is that I have multiple rows of long and lat in the from data set and multiple long and lat in the to dataset.

long and lat from from dataset             long and lat from the to dataset

(200,210)                                             (190,210)

(400,310)                                             (300,216)

(401,432)                                             (200,202)

(501,301)

.

.

.

.

(100.300)

Now I want the distance between (200,210)  and   (190,210) then

between (200,210) and (300,216) then

between (200,210) and (200,202) then

between (400,310) and (190,210) and so forth.

Thank you!

Discussion stats
• 3 replies
• 87 views
• 2 likes
• 3 in conversation