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*/
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.
@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).
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!
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.