BookmarkSubscribeRSS Feed
fp
Calcite | Level 5 fp
Calcite | Level 5
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;
2 REPLIES 2
data_null__
Jade | Level 19
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=_:) idgroup(min(dif) out(b)=);
run;
proc print;
run;
[/pre]
ballardw
Super User
Does this do what you want?

proc sql;
select *
from new
where abs(50-b) =(select min(abs(50-b)) from new);
quit;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1205 views
  • 0 likes
  • 3 in conversation