Dear All,
I want to find out 8th and 29th smallest value from data.
Please guide me.
Please find attached sample data.
Thanks,
You can identify the Kth obs by sorting in ascending order and specifying its obs number in _n_.
data have;
input values;
datalines;
1.01
1.17
0.97
1.26
1.19
1.12
0.76
0.87
0.72
0.94
0.89
0.84
0.85
0.98
0.81
1.06
1
0.94
0.79
0.91
0.76
0.98
0.93
0.88
0.9
1.04
0.86
1.12
1.06
1
0.81
0.94
0.78
1.01
0.95
0.9
;
run;
proc sort data=have;
by values;
run;
data want;
set have;
if _n_=8 or _n_=29 then do;
order=cats(_n_,'th');
output;
end;
run;
Finding k-th smallest values has been discussed multiple times, the problem is even part of papers. So please search and adapt the things you find.
You can identify the Kth obs by sorting in ascending order and specifying its obs number in _n_.
data have;
input values;
datalines;
1.01
1.17
0.97
1.26
1.19
1.12
0.76
0.87
0.72
0.94
0.89
0.84
0.85
0.98
0.81
1.06
1
0.94
0.79
0.91
0.76
0.98
0.93
0.88
0.9
1.04
0.86
1.12
1.06
1
0.81
0.94
0.78
1.01
0.95
0.9
;
run;
proc sort data=have;
by values;
run;
data want;
set have;
if _n_=8 or _n_=29 then do;
order=cats(_n_,'th');
output;
end;
run;
Alternatively
proc summary data = have;
var values;
output out = want(keep = values_8 values_29) idgroup (max(values) out[29] (values) = );
run;
This is a great solution I wish I had known about before (I really must go back and review proc summary).
BUT ... I think you need to replace "max(values)" with "min(values)"
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!
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.