Help using Base SAS procedures

how to select top 2 rows

Reply
Contributor sss
Contributor
Posts: 65

how to select top 2 rows

data temp;
input csid asset cost;
datalines;
1001 123 10
1001 254 30
1001 554 15
2001 235 05
2001 100 10
2001 145 15
2001 470 60
5101 235 02
5101 478 10
2000 123 10
2000 456 10
;
run;

HI

I want to select top two rows form each CSID variable by whose cost is higher
Regular Contributor
Posts: 151

Re: how to select top 2 rows

Hello.
Try this code.
[pre]
proc sort data=temp; by csid descending cost; run;
data temp2; retain n 0; set temp; by csid descending cost;
if first.csid then n=1; else n=n+1;
if n le 2;
run;

[/pre]
Contributor sss
Contributor
Posts: 65

Re: how to select top 2 rows

Thnx you a lot it works
Super User
Posts: 9,687

Re: how to select top 2 rows

If you do not want ties.


[pre]
data temp;
input csid asset cost;
datalines;
1001 123 10
1001 254 30
1001 554 15
2001 235 05
2001 100 10
2001 145 15
2001 470 60
5101 235 02
5101 478 10
2000 123 10
2000 456 10
;
run;
ods output extremevalues=nextrval(keep=csid high) ;
proc univariate data=temp nextrval=2;
class csid;
var cost;
run;

proc print;run;
[/pre]



Ksharp Message was edited by: Ksharp
Ask a Question
Discussion stats
  • 3 replies
  • 914 views
  • 0 likes
  • 3 in conversation