data have;
input stdid Marks;
cards;
1 34
1 43
2 54
2 60
2 65
;
run;
proc sql ;
select *
FROM have h1
where marks=(select MIN(Marks) from have h2 where h1.stdid=h2.stdid)
order BY stdid;
quit;
Nope, doesn't exist. 😞
You need to find some other logic, how is your first record identified?
data have;
input stdid Marks;
cards;
1 34
1 43
2 54
2 60
2 65
;
run;
proc sql ;
select *
FROM have h1
where marks=(select MIN(Marks) from have h2 where h1.stdid=h2.stdid)
order BY stdid;
quit;
Won't be the same if there is dups
data have;
input Stdid Marks;
cards;
1 34
1 34
1 43
2 54
2 60
2 65
;
If no more columns
proc sql ;
select distinct *
FROM have h1
where marks=(select MIN(Marks) from have h2 where h1.stdid=h2.stdid)
order BY stdid
;
quit;
At the risk of being obnoxious, what I was trying to say is not you can generate the same results, rather, is that your SQL logic is not to mirror 'first.', as you have heavily depended on some other assumptions. What if this:
data have;
input Stdid Marks;
cards;
1 36
1 34
1 43
2 54
2 60
2 65
;
Yes, i am heavily depend on the assumptions of marks ordered.
Anyway, @Ps8813 too much sweet is harmful!
I am second to @Reeza's comments, you can't get it done using Proc SQL without involving some 'extraordinary measures', I mean 'undocumented/unsupported feature', such as the following:
proc sql;
create table test as
select * from have
group by stdid
having monotonic()=min(monotonic())
;
quit;
monotonic() is not officially supported by SAS, use it at your own risk. However, this implementation is the closest I can think of to your data step counterpart 'first.'.
Thank u all for quick replying. Both below codes are working.
1:-
proc sql;
select * from test group by stdid having monotonic()=min(monotonic());
quit;
2:-
proc sql ;
select *
FROM test h1
where marks=(select MIN(Marks) from test h2 where h1.stdid=h2.stdid)
order BY stdid;
quit;
Though second solution wont give correct result when we have duplicate marks for same stdid.
Thanks again 🙂
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.