Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

USE of 2D arrays in SAS for Distance computation??

Status
Not open for further replies.

hcs301080

Programmer
Jun 5, 2003
3
US
Hi,
I wanted to write a SAS code for computing distance given the latitude and longitude of the places....( I got around 1000+ locations and I want to find the distance between each other).

I tried using the single dimension array but it seems to be generating erroneous values.
So resorted to 2D array....someone suggested that I use this logic
START will keep track of which city */
/* is the current "from". Calculate the distance to all */
/* the other "to" cities. */


START=1;
DO I=1 TO 1222;
DO J=1 TO (START-1),(START+1) TO 1222; /*?PLEASE HELP ME UNDERSTAND THIS STATEMENT ??*/
X1=lon (START);
Y1=lat(START);
X2=lon(J);
Y2=lat(J);

DIST(I,J)=4000*ARCOS(SIN(Y2)*SIN(Y1)+COS(Y2)*COS(Y1)*COS(X2-X1) );
/*the formula can be changed as per the requirement and the results observed*/;
END;

Can anyone understand this statement in the above code
DO J=1 TO (START-1),(START+1) TO 1222;

Its a bit weird to visulalise the 2D array in SAS in the above code

cheers
 
1. This is not a SAS question. It is the logic behind your SAS code.

2. I assume you want to calculating distance between every of your 1222 locations. After calculate distance from location a to location b, you don't have to calculate distance from location b to location a. And distance from location a to location a is 0.
Let's look at a 3-location example:

From/To L1 L2 L3
L1 D11 D12 D13
L2 D21 D22 D23
L3 D31 D32 D33

You need to calculated D12 D13 D23.
Which is:
do i = 1 to 3;
do j = i + 1 to 3;
x1 = lon(i);
y1 = lat(i);
x2 = lon(j);
y2 = lat(j);
DIST(I,J)=4000*ARCOS(SIN(Y2)*SIN(Y1)+COS(Y2)*COS(Y1)*COS(X2-X1) ); /* whatever the function is*/
end;
end;

When i = 1, first j is 1 + 1 = 2 you get d12; second j is 3 you get d13. The first j loop ended;
When i = 2, first j is 2 + 1 = 3 you get d23. This is the end og j loop.
When i = 3 there is no j loop.

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top