BookmarkSubscribeRSS Feed
deega
Quartz | Level 8

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.....

18 REPLIES 18
stat_sas
Ammonite | Level 13

Hi,

 

What do you mean by a particular record? 

deega
Quartz | Level 8
@stat_sas
by particular record, i mean for each record, something like below
Record No. Nearest Record 1 Nearest Record 2 Nearest Record 3
1 5 6 7
2 1 2 3
3 2 1 6
4 5 1 6
5 : : :
6 : : :
7 : : :
8 : : :
9 : : :
10 : : :
:
:

stat_sas
Ammonite | Level 13

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.

KachiM
Rhodochrosite | Level 12

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.

Reeza
Super User

Is there any other way besides calculating all distances to identify the shortest 3 distances?

deega
Quartz | Level 8
@Reeza This is what my question is... Few days back I used SPSS Modeler, using its KNN Node, we can quickly find the nearest record ...
Reeza
Super User

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.

Rick_SAS
SAS Super FREQ

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."

deega
Quartz | Level 8
@Rick_SAS
Thanks for the reply. I have not used arrays much and trying to understand your program. Could you please tell what is _N_ here ???
Reeza
Super User

_n_ is an automatic variable that counts the number of boundary steps. It is typically used as a pseudo row counter. 

 

http://support.sas.com/documentation/cdl/en/lrcon/69852/HTML/default/viewer.htm#p0e0mk25gs9binn1s9ji...

deega
Quartz | Level 8
@Rick_SAS If I have around 1000000 records and for each record I need at least three nearest records,( means KNN with K=3) will this work? The Distance proc is not working because the output is 1000000 * 1000000 matrix.
Rick_SAS
SAS Super FREQ

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.

deega
Quartz | Level 8

@Rick_SAS

The shape of my dataset is as follows:

S. No.Var1Var2Var3Var4Var5::Var30
A1121315
A2211312
A3112121
A4233121
A5121212
A6313311
A7212121
A8121221
A9113522
A10::221000
A1000000152034

 

 

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.

 

Reeza
Super User

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. 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

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
  • 18 replies
  • 2212 views
  • 1 like
  • 6 in conversation