DATA Step, Macro, Functions and more

SAS function to get closet value to x

Reply
fp
N/A
Posts: 1

SAS function to get closet value to x

Hi SAS users,

Is there any SAS function identifying the closest value to 50 in the variable B
in the a dataset below. If not, any other way to get the value without using SAS function.

Thank you
FP

data new;
input A B;
datalines;
1 50.10
1 50.2
2 49.99
2 49.7
3 49.8
3 49.90
4 50.7
4 50.3
5 50.5
5 50.6
;
run;
Respected Advisor
Posts: 3,799

Re: SAS function to get closet value to x

If you use ABS(B-50) as a distance then find MIN would that be adequate?

[pre]
data new;
input A B;
dif = abs(b-50);
datalines;
1 50.10
1 50.2
2 49.99
2 49.7
3 49.8
3 49.90
4 50.7
4 50.3
5 50.5
5 50.6
;
run;
proc summary nway;
class a;
output out=work.closest(drop=_Smiley Happy idgroup(min(dif) out(b)=);
run;
proc print;
run;
[/pre]
Super User
Posts: 11,343

Re: SAS function to get closet value to x

Does this do what you want?

proc sql;
select *
from new
where abs(50-b) =(select min(abs(50-b)) from new);
quit;
Ask a Question
Discussion stats
  • 2 replies
  • 145 views
  • 0 likes
  • 3 in conversation