turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- Distance calculation

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-09-2018 09:34 AM

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*/

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to mauri0623

03-09-2018 10:49 AM

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.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to mauri0623

03-09-2018 03:01 PM

@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).

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ballardw

03-12-2018 09:47 AM

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!