Hi Tom,
First of all, thanks a lot for taking the time for this, very much appreciated. I’ll provide you with all the information you need. Just want to mention that I simplified the macro for you, and change variable names and datasets for simplification purpose. So it is probably (not very, I hope so) that if you run this macro you could probably get some error.
Let’s say I’ve one object with multiple characteristics (variables), and I want to compare it with other objects on the same characteristics using the Euclidean distance.
%macro example (vars);
data one;
input mylib.one (keep = id &vars); /*One record only*/
run;
data many;
input mylib.many (keep = id &vars); /*Multiple records*/
run;
*Counting the # of specified variables in the ‘vars’ parameter and creating a var list;
proc transpose data=one out=z (drop=_label_ );
var &var;
run;
proc sql noprint;
select distinct _name_ into :var_list separated by " "
from z;
quit;
%let number_vars = &sqlObs;
*Assigning macro values to each variable in the &vars parameter;
data _null_ ;
set z ;
call symputx(cats("value",_n_),col1) ;
run;
*Preparing dataset the calculation of the Euclidean distance;
data all;
set one many;
run;
*Calculating the Euclidean distance;
proc distance data=all out=Dist1 (keep= dist1) method=Euclid;
var interval(&vars);
run;
data dist2;
set dist1;
if _n_=1 then delete;
rename dist1 = d;
run;
*Matching each object with a distance value;
data many_dist;
merge many dist2;
*Renaming &vars before merging back with ‘One’;
%do i = 1 %to &number_vars.;
%scan(&vars.,&i.) = key_&i.;
%end;
run;
*Getting the final dataset;
*This dataset will contain in the first records and 1+& number_vars columns,
the information from ‘One’. The subsequent columns (and as many records
as in the dataset ‘many’ the information from many_dist.;
data final
merge on many_dist;
*Here is where I want to calculate the difference between
&value1-key_1, &value2-key_2 … &valueK-key_K using arrays.
As you can notice the values &value1 to &valueK are the same that appear
in the second to +& number_vars columns, but only in the first row.;
run;
%mend;
%example (vars= a b c); *The # of variables in the ‘vars’ parameter can change;
I hope this is clear enough. Looking forward to getting your inputs.
Thanks again,
AG.
... View more