Q:

sas sql first obs last obs

I have a dataset where I have information about alle the courses a person have taken. I'm interested in information about the first course they began (start date and end date) and also start and end date on the last course they began. The courses can overlap and there can be a large number of courses on each person. 
I want to edit the data so that one person is one observation. 
First I sorted the data like this:
by personid startdate descending enddate;

And the I tried this:
proc sql;
create table new as
select distinct personid, 
count(*) as number_courses, 
min(startdate) as first_startdate 'First startdate' format date9., min    (enddate) as first_enddate 'First enddate' format date9.,
max(startdate) as last_startdate 'Last startdate' format date9., max(enddate) as last_enddate 'Last enddate'  format date9.
from old
group by personid
;
quit;

But this doesn't give me what I want. It typically is the min(enddate) which is wrong. I want the enddate that corresponds to the minimum value of startdate and it is not nesesarilly the minimum enddate.
In stead I wrote:
data first last;
set old;
by personid startdate descending enddate;
if first.personid then output first;
if last.personid then output last;
keep personid startdate enddate ;
run;

This gave me want I wanted. Then I had to rename the varibles and left join the  dataset last on first via personid. 
But I don't find this method optimal.
So my question is. What should I change in in proc sql to get the result that I want?

A:

I would actually use PROC SUMMARY for this (the same as PROC MEANS with the NOPRINT option).  You can take advantage of the MINID and MAXID functions to give you what you want. In the code, MINID says 'give me the startdate and endate associated with the earliest startdate', MAXID obviously gives the values associated with the latest startdate
data have; /* sample data */
input personid startdate :date9. enddate :date9.;
format startdate enddate date9.;
datalines;
1   01jan2014 01mar2014
1   25feb2014 01jun2014
1   03mar2014 25may2014
2   01may2014 01sep2014
2   02sep2014 01oct2014
2   01nov2014 01dec2014
;
run;

proc summary data=have nway;
class personid;
output out=want (drop=_:)
    n(personid) = number_courses 
    minid(startdate(startdate enddate))=first_startdate first_enddate
    maxid(startdate(startdate enddate))=last_startdate last_enddate;
run;

