Hi All,
I have a dataset with around 100000 records. I want to know only those three records that have shortest distance with a particular record. If I run the distance procedure then the program generates the output of matrix of 100000 variables giving distance of a variable with all other variables. It is actually not required and unecessarily it eats up my system time and sometimes evenhangs my system. If there is any other procedure that gives me the variables with just the shortest distance, please share.
Thanks in advance.....
Hi,
What do you mean by a particular record?
Not sure but one way to get that is using k means clustering. Make a reference value and use that in proc fastclus in instat option to get the distances with regard to reference value. Then sort the output data set to flag the top 3 closest values.
Deega:
1 5 6 7
2 1 2 3
3 2 1 6
4 5 1 6
In this sample data set, you say 5, 6, 7 are the records that are closer to Record 1. You show a small example as how you got 5, 6, and 7?
What variable(s) (not shown here) are used for this decision?
Provide an example and illustrate how you find them. Finding algorithm to do fast is simpler once your your example is clear.
Is there any other way besides calculating all distances to identify the shortest 3 distances?
It was a logical question. You can't find the nearest neighbour without calculating all values first. Perhaps the KNN process in SPSS precalculates the distance and then obtains the nearest neighbour when you request it. This means it isn't calculating the distance matrix every time.
You need to provide more details regarding what is the issue in your current process. Ideally, you can provide sample input, your code and a message with which step is inefficient. Then we'll be able to suggest alternatives for your.
PROC DISTANCE is relatively fast, but if you don't need it run each time and can cache the results somehow that's a better process.
I assume you want the Euclidean distance.
If you know the "particular record", then this is an easy problem that you can complete in the DATA step.
1. Put the particular record first in the data set (or hard-code it into an array).
2. Use the EUCLID function to compute the Euclidean distance between the particular record and the others.
3. Sort the data by distance and use the first 3 records.
For example, the following DATA step computes the Euclidean distances between the numerical measurements for the first observation ("Alfred") and the other observations. The result shows that Mary, William, and Janet are the students whose numerical measurements are closest to Alfred, as measured by the Euclidean distance function:
data Dist(drop=i);
array refPt [3] _temporary_; /* automatically RETAINed */
array diff [3] _temporary_;
set sashelp.class;
array Pt [*] Height Weight Age;
/* initialize refPt, or use
array refPt[3] (val1 val2 val3); */
if _N_ = 1 then
do i = 1 to dim(Pt);
refPt[i] = Pt[i];
end;
/* compute difference between Pt and refPt */
do i = 1 to dim(pt);
diff[i] = refPt[i] - Pt[i];
end;
dist = euclid(of diff[*]);
if _N_>1;
run;
proc sort data=Dist;
by dist;
run;
proc print data=Dist(obs=3);
run;
You can generalize this problem. Instead of finding the points nearest to a single reference point, you can find the points in one group that are nearest to points in another group. See the article "Distances between observations in two groups."
_n_ is an automatic variable that counts the number of boundary steps. It is typically used as a pseudo row counter.
Please clarify two issue:
1. How many variables in this problem?
2. Do you have ONE reference point, and you want the three closest from among 1M observations? Or do you have 1M points and for each of those you want to find the three nearest neighbors? The first case requires computing 1M distances, The second case requires computing 1E12 distances.
The shape of my dataset is as follows:
S. No. | Var1 | Var2 | Var3 | Var4 | Var5:: | Var30 |
A1 | 1 | 2 | 1 | 3 | 1 | 5 |
A2 | 2 | 1 | 1 | 3 | 1 | 2 |
A3 | 1 | 1 | 2 | 1 | 2 | 1 |
A4 | 2 | 3 | 3 | 1 | 2 | 1 |
A5 | 1 | 2 | 1 | 2 | 1 | 2 |
A6 | 3 | 1 | 3 | 3 | 1 | 1 |
A7 | 2 | 1 | 2 | 1 | 2 | 1 |
A8 | 1 | 2 | 1 | 2 | 2 | 1 |
A9 | 1 | 1 | 3 | 5 | 2 | 2 |
A10:: | 2 | 2 | 1 | 0 | 0 | 0 |
A1000000 | 1 | 5 | 2 | 0 | 3 | 4 |
I want KNN (K=3) for each observation from A1 to A1000000. Actually I had 200M observations, first I made clusters using fastclus and thought of using distance procedure for calculating distance and sort and take the top 3 records. Some clusters have less records but some still have high number of records. I was able to use distance procedure till 100,000 records but it is not responding above this limit. When I tried KNN Node in SPSS it responded to higher number of records too, so I thought of customising program for distance instead of using Distance proc.
Please advise me something for this situation.
Customizing only makes sense if you have some other manner of filtering your calculations. For example, if this was spatial you might limit it to neighbouring provinces/states.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.