BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jitendrakoli
Fluorite | Level 6

Dear All,

 

I want to find out 8th and 29th smallest value from data.

 

Please guide me.

 

Please find attached sample data.

 

Thanks,

1 ACCEPTED SOLUTION

Accepted Solutions
japelin
Rhodochrosite | Level 12

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;

View solution in original post

5 REPLIES 5
andreas_lds
Jade | Level 19

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.

japelin
Rhodochrosite | Level 12

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;
PeterClemmensen
Tourmaline | Level 20

Alternatively

 

proc summary data = have;
   var values;
   output out = want(keep = values_8 values_29) idgroup (max(values) out[29] (values) = );
run;
mkeintz
PROC Star

@PeterClemmensen :

 

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)"

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Ksharp
Super User
Do you consider TIES value ?

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 5 replies
  • 643 views
  • 5 likes
  • 6 in conversation