DATA Step, Macro, Functions and more

First. In proc sql

Accepted Solution Solved
Reply
Contributor
Posts: 24
Accepted Solution

First. In proc sql

Hi , i have a small and sweet question.
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
Solution
‎02-09-2016 12:33 PM
Super Contributor
Posts: 490

Re: First. In proc sql

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;

View solution in original post


All Replies
Super User
Posts: 19,772

Re: First. In proc sql

Nope, doesn't exist. Smiley Sad

 

You need to find some other logic, how is your first record identified?

Solution
‎02-09-2016 12:33 PM
Super Contributor
Posts: 490

Re: First. In proc sql

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;
Respected Advisor
Posts: 3,156

Re: First. In proc sql

Posted in reply to mohamed_zaki

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
;
Super Contributor
Posts: 490

Re: First. In proc sql

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;
Respected Advisor
Posts: 3,156

Re: First. In proc sql

Posted in reply to mohamed_zaki

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
;
Super Contributor
Posts: 490

Re: First. In proc sql

Yes, i am heavily depend on the assumptions of marks ordered.

Anyway, @Ps8813 too much sweet is harmful!

Respected Advisor
Posts: 3,156

Re: First. In proc sql

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

 

Super Contributor
Posts: 490

Re: First. In proc sql

Contributor
Posts: 24

Re: First. In proc sql

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 Smiley Happy

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 300 views
  • 1 like
  • 4 in conversation