BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ddemilla
Fluorite | Level 6

I am calculating pairwise geodesic distances between street address using latitude and longitude. I would like to use "proc distance" to automatically do this for me. Does anyone know if geodesic distance can be calculated using proc distance?

 

data have;
format latitude 8.5;
format longitude 8.5;
   input Application_id $10. Latitude Longitude;
   datalines;
1000000999 -75.0000 50.0000
1000000888 -90.0000 30.0000
1000000777 -75.0000 50.0500
1000000666 -75.0000 50.0400
1000000555 -80.0000 35.0000
1000000444 -75.0300 50.0000
;

Want:

dist_1000000999 dist_1000000888 dist_1000000777 dist_1000000666 dist_1000000555 dist_1000000444 id_variable
0           dist_1000000999
11391.48746 0         dist_1000000888
0.898135642 11391.48746 0       dist_1000000777
0.718508521 11391.48746 0.179627134 0     dist_1000000666
410.9346807 11738.41243 411.3264781 411.2480465 0   dist_1000000555
2.081141049 11393.5686 2.266323028 2.201452738 409.062496 0 dist_1000000444

 

Any help is much appreciated,

Daniel

1 ACCEPTED SOLUTION

Accepted Solutions
MikeZdeb
Rhodochrosite | Level 12

Hi, doesn't give you a matrix but gives you the values in a data set.  

 

data have;
input id $10. latitude longitude;
datalines;
1000000999 -75.0000 50.0000
1000000888 -90.0000 30.0000
1000000777 -75.0000 50.0500
1000000666 -75.0000 50.0400
1000000555 -80.0000 35.0000
1000000444 -75.0300 50.0000
;

 

data want;
format latitude longitude la2 lo2 10.5 distance 15.5;
set have nobs=howmany;
do j=_n_+1 to howmany;
   set have (rename=(id=id2 latitude=la2 longitude=lo2)) point=j; 
   distance = geodist(latitude,longitude,la2,lo2,'m');
   output;
end;
run;

 

data set WANT ... ID ID2 and distance in miles

 

1 1000000999 1000000888 11391.48746
2 1000000999 1000000777 0.89814
3 1000000999 1000000666 0.71851
4 1000000999 1000000555 410.93468
5 1000000999 1000000444 2.08114
6 1000000888 1000000777 11391.48746
7 1000000888 1000000666 11391.48746
8 1000000888 1000000555 11738.41243
9 1000000888 1000000444 11393.56860
10 1000000777 1000000666 0.17963
11 1000000777 1000000555 411.32648
12 1000000777 1000000444 2.26632
13 1000000666 1000000555 411.24805
14 1000000666 1000000444 2.20145
15 1000000555 1000000444 409.06250

 

If you really want a matrix with those zeroes in the diagonal, first change the loop in the data step as follows ...

 

from ... do j=_n_+1 to howmany;

to ... do j=_n_ to howmany;

 

then do the following ...

 

proc transpose data=want out=want (drop=_name_) prefix=d;
var distance;
id id2;
by id notsorted;
run;

 

and you get ...

 

Obs        id         d1000000999     d1000000888     d1000000777     d1000000666     d1000000555     d1000000444

 1     1000000999         0.00000     11391.48746         0.89814         0.71851       410.93468         2.08114
 2     1000000888          .              0.00000     11391.48746     11391.48746     11738.41243     11393.56860
 3     1000000777          .               .              0.00000         0.17963       411.32648         2.26632
 4     1000000666          .               .               .              0.00000       411.24805         2.20145
 5     1000000555          .               .               .               .              0.00000       409.06250
 6     1000000444          .               .               .               .               .              0.00000

View solution in original post

2 REPLIES 2
MikeZdeb
Rhodochrosite | Level 12

Hi, doesn't give you a matrix but gives you the values in a data set.  

 

data have;
input id $10. latitude longitude;
datalines;
1000000999 -75.0000 50.0000
1000000888 -90.0000 30.0000
1000000777 -75.0000 50.0500
1000000666 -75.0000 50.0400
1000000555 -80.0000 35.0000
1000000444 -75.0300 50.0000
;

 

data want;
format latitude longitude la2 lo2 10.5 distance 15.5;
set have nobs=howmany;
do j=_n_+1 to howmany;
   set have (rename=(id=id2 latitude=la2 longitude=lo2)) point=j; 
   distance = geodist(latitude,longitude,la2,lo2,'m');
   output;
end;
run;

 

data set WANT ... ID ID2 and distance in miles

 

1 1000000999 1000000888 11391.48746
2 1000000999 1000000777 0.89814
3 1000000999 1000000666 0.71851
4 1000000999 1000000555 410.93468
5 1000000999 1000000444 2.08114
6 1000000888 1000000777 11391.48746
7 1000000888 1000000666 11391.48746
8 1000000888 1000000555 11738.41243
9 1000000888 1000000444 11393.56860
10 1000000777 1000000666 0.17963
11 1000000777 1000000555 411.32648
12 1000000777 1000000444 2.26632
13 1000000666 1000000555 411.24805
14 1000000666 1000000444 2.20145
15 1000000555 1000000444 409.06250

 

If you really want a matrix with those zeroes in the diagonal, first change the loop in the data step as follows ...

 

from ... do j=_n_+1 to howmany;

to ... do j=_n_ to howmany;

 

then do the following ...

 

proc transpose data=want out=want (drop=_name_) prefix=d;
var distance;
id id2;
by id notsorted;
run;

 

and you get ...

 

Obs        id         d1000000999     d1000000888     d1000000777     d1000000666     d1000000555     d1000000444

 1     1000000999         0.00000     11391.48746         0.89814         0.71851       410.93468         2.08114
 2     1000000888          .              0.00000     11391.48746     11391.48746     11738.41243     11393.56860
 3     1000000777          .               .              0.00000         0.17963       411.32648         2.26632
 4     1000000666          .               .               .              0.00000       411.24805         2.20145
 5     1000000555          .               .               .               .              0.00000       409.06250
 6     1000000444          .               .               .               .               .              0.00000

ddemilla
Fluorite | Level 6

Thank you, exactly what I was looking for!

sas-innovate-2024.png

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.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 1704 views
  • 0 likes
  • 2 in conversation