Statistical programming, matrix languages, and more

Calculate distance between two vectors

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 122
Accepted Solution

Calculate distance between two vectors

I have a dataset such as below.

 

ID     Base     x         y         z

01      0         1.1    2.3        2

02      0         1.9    3.3        2.3

03      1         5.1    2.5        6

04      0         1.1    4.3        4

05      0         3.1    2.3        5

06      0         1.7    1.3        2

 

Use Base=1 as the main ID, I want to calculate a Euclidean distance based on x, y and z. That is, I want to calculate the distance between each of ID whose base=0 and base=1 (ID 03) based on x,y ,z. I want to add one column dist, which is the distance between base=0 and base=1. The distance is defined as square root of sum of vector(x,y,z).

 

For example, distance between ID01 and ID03 is distance between [1.1, 2.3, 2] and [5.1, 2.5, 6]. I want to get a new dataset as below.

 

ID     Base     x         y         z          dist

01      0         1.1    2.3        2          distance(ID01,ID03)

02      0         1.9    3.3        2.3       distance(ID02,ID03)

03      1         5.1    2.5        6          distance(ID03,ID03)=0

04      0         1.1    4.3        4          distance(ID04,ID03)

05      0         3.1    2.3        5          distance(ID05,ID03)

06      0         1.7    1.3        2          distance(ID06,ID03)

 

Thanks.


Accepted Solutions
Solution
‎01-17-2017 11:42 PM
Grand Advisor
Posts: 16,306

Re: Calculate distance between two vectors

Try using PROC distance and filtering the results. 

View solution in original post


All Replies
Solution
‎01-17-2017 11:42 PM
Grand Advisor
Posts: 16,306

Re: Calculate distance between two vectors

Try using PROC distance and filtering the results. 

SAS Super FREQ
Posts: 3,222

Re: Calculate distance between two vectors

See the article "Distances between observations in two groups."  For your data, one group contains a single observation (03) and the other group contains the other observations.

 

For a fun application of distances between groups, see the article 'Is "La Quinta" Spanish for "Next to Denny's"?'

Post a Question
Discussion Stats
  • 2 replies
  • 188 views
  • 2 likes
  • 3 in conversation