BookmarkSubscribeRSS Feed
macworthy
Calcite | Level 5

I am runing a KNN algorithm and would like to be able to extract the distance for each tested variable. For example, if the data is trained on Dataset A and tested on Dataset B, then what is the distance between a given record in Dataset B and its nearest neighbor in Dataset A. I am using the PROC DISCRIM procedure to execute this. 

 

 

DATA MASTER;
	INFILE DATALINES;
	INPUT k  errorrate;
	DATALINES;
RUN;

%macro KNND;
	%do i=1 %TO 50;
		%IF %SYSFUNC(MOD(&i,2))=1 %THEN %DO;

			PROC DISCRIM 
				DATA=TRAIN 
				TESTDATA=TEST
				TESTOUT=_score&i
				METHOD=NPAR
				K=&i;
				CLASS BAD;
				VAR VAR1 VAR2 VAR3 VAR4; 
			RUN; 

				PROC SQL noprint;
					CREATE TABLE stage&i AS 
						SELECT &i AS k,
						SUM(CASE WHEN BAD = _INTO_ THEN 0 ELSE 1 END)/COUNT(NIIN) AS errorrate
				FROM _score&i;
			QUIT;

			PROC APPEND BASE=MASTER DATA=stage&i;
			RUN; 


			%END;


%END; 

%MEND KNND;

%KNND

 

1 REPLY 1
PGStats
Opal | Level 21

You could concatenate datasetA and datasetB and extract a sub-matrix of the matrix provided by proc distance.

PG

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 1706 views
  • 0 likes
  • 2 in conversation