## min obs

Solved
Occasional Contributor
Posts: 7

# min obs

i have a dataset like following

data min;

input stid marks;

101 98

102 87

103 58

100 52

;

question::  i want least stid along with his marks using sql query...........

Accepted Solutions
Solution
‎05-02-2018 01:50 AM
PROC Star
Posts: 1,584

## Re: min obs

[ Edited ]
``````data min;
input stid marks;
datalines;
101 98
102 87
103 58
100 52
;

proc sql;
create table want as
select stid,(select min(marks) from min) as marks
from min
where calculated marks=marks;
quit;

``````

I disagree with proc sort. Rather, look at proc rank.

All Replies
PROC Star
Posts: 1,584

## Re: min obs

his marks? meaning whose marks? What is your expected output?

Occasional Contributor
Posts: 8

## Re: min obs

You want the stid and marks variables, but only for for the stid with the lowest marks value? A proc sort with the output only having 1 observation could do it. However it will only give you one result even if multiple stid have the same lowest marks value.

``proc sort data=min out=lowest_marks (obs=1);by marks;run;``

If you want all of the stid that have the same lowest marks value you can use an sql subquery to find the lowest marks value, and inner join on that to get all the relevant stid.

``````proc sql;
create table lowest_marks as
select *
from min as a
inner join (select min(marks) as lowest_marks from min) as b
on a.marks = b.lowest_marks
;quit;``````
Solution
‎05-02-2018 01:50 AM
PROC Star
Posts: 1,584

## Re: min obs

[ Edited ]
``````data min;
input stid marks;
datalines;
101 98
102 87
103 58
100 52
;

proc sql;
create table want as
select stid,(select min(marks) from min) as marks
from min
where calculated marks=marks;
quit;

``````

I disagree with proc sort. Rather, look at proc rank.

Super User
Posts: 10,689

## Re: min obs

``````data min;
input stid marks;
datalines;
101 98
102 87
103 58
100 52
;

proc sql;
select *
from min
having marks=min(marks);
quit;``````
☑ This topic is solved.