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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.