- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I want to change below code in proc sql.
Data test;
Set marks;
By stdid;
If first.stdid;
Run;
Marks
____________
Stdid. Marks
1. 34
1. 43
2. 54
2. 60
2. 65
Test
________________
Stdid. Marks
1. 34
2. 54
Thanks
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Nope, doesn't exist. 😞
You need to find some other logic, how is your first record identified?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Yes, i am heavily depend on the assumptions of marks ordered.
Anyway, @Ps8813 too much sweet is harmful!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.'.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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 🙂