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...........
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.
his marks? meaning whose marks? What is your expected output?
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;
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.
data min;
input stid marks;
datalines;
101 98
102 87
103 58
100 52
;
proc sql;
select *
from min
having marks=min(marks);
quit;
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.