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;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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