closest value into a macro variable

#1
Hi everyone,

I have a numeric variable, lets call it 'times'. I have a macro variable 'myTime' defined to be some value, lets say 200. I would like to extract the closest value in 'times' to 'myTime' as a new macro variable 'closestTime'.

Would anyone know how to modify the code below so it gives the closest time instead of the smallest difference? (Or know of some other way I could extract the closest time into a macro variable?). Help is much appreciated!

proc sql noprint;
select min(abs(times-&myTime) )
into :closestTime
from myData;
quit;

thank you!
 
#2
One simple way would be to do it in a data step if your data in not huge
Something like this should work..
Code:
data _null_;
set myData(end=last);
retain mindiff 0;
diff=abs(times-&myTime);
mindiff=min(diff,mindiff);
if last call symput("closestTime",mindiff);
run;
PS: Code not tested