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

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...........

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
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. 

View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20

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

JChambo
Fluorite | Level 6

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

Ksharp
Super User
data min;
input stid marks;
datalines;
101 98
102 87
103 58
100 52
;

proc sql;
select *
from min
having marks=min(marks);
quit;

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
  • 4 replies
  • 1085 views
  • 0 likes
  • 4 in conversation