Hi
I am trying to get MIN date and MAX date; I can do with Proc Sql, but was wondering is there a way to do with DATA STEP
Thanks
example below;
DATA HAVE
SUBJID | VISITNUM | VISIT | FIRSTDATE | LASTDATE |
1 | 20 | BASELINE (RAW) | 13-May-10 | 14-Jun-10 |
1 | 30 | MONTH 1 | 15-Jun-10 | 12-Aug-10 |
1 | 40 | MONTH 3 | 13-Aug-10 | 11-Nov-10 |
1 | 50 | MONTH 6 | 12-Nov-10 | 12-May-11 |
1 | 70 | MONTH 12 | 13-May-11 | 10-Nov-11 |
1 | 90 | MONTH 18 | 11-Nov-11 | 8-May-12 |
1 | 110 | MONTH 24 | 9-May-12 | 13-Nov-12 |
1 | 130 | MONTH 30 | 14-Nov-12 | 21-May-13 |
1 | 150 | MONTH 36 | 22-May-13 | 1-Aug-13 |
DATA NEED:
SUBJID | VISITNUM | VISIT | FIRSTDATE | LASTDATE | MIN FIRSTDATE | MAX LASTDATE |
1 | 20 | BASELINE (RAW) | 13-May-10 | 14-Jun-10 | 13-May-10 | 1-Aug-13 |
1 | 30 | MONTH 1 | 15-Jun-10 | 12-Aug-10 | 13-May-10 | 1-Aug-13 |
1 | 40 | MONTH 3 | 13-Aug-10 | 11-Nov-10 | 13-May-10 | 1-Aug-13 |
1 | 50 | MONTH 6 | 12-Nov-10 | 12-May-11 | 13-May-10 | 1-Aug-13 |
1 | 70 | MONTH 12 | 13-May-11 | 10-Nov-11 | 13-May-10 | 1-Aug-13 |
1 | 90 | MONTH 18 | 11-Nov-11 | 8-May-12 | 13-May-10 | 1-Aug-13 |
1 | 110 | MONTH 24 | 9-May-12 | 13-Nov-12 | 13-May-10 | 1-Aug-13 |
1 | 130 | MONTH 30 | 14-Nov-12 | 21-May-13 | 13-May-10 | 1-Aug-13 |
1 | 150 | MONTH 36 | 22-May-13 | 1-Aug-13 | 13-May-10 | 1-Aug-13 |
OR
SUBJID | MIN FIRSTDATE | MAX LASTDATE |
1 | 13-May-10 | 1-Aug-13 |
You can do it fairly easily using a DOW loop. e.g.:
data need;
do until (last.subjid);
set have;
by subjid;
min_firstdate=min(min_firstdate,firstdate);
max_lastdate=max(max_lastdate,lastdate);
end;
format min_firstdate max_lastdate date9.;
do until (last.subjid);
set have;
by subjid;
output;
end;
run;
Yes you can, but why? SQL is much easier to code.
With the data step use retain.
You can do it fairly easily using a DOW loop. e.g.:
data need;
do until (last.subjid);
set have;
by subjid;
min_firstdate=min(min_firstdate,firstdate);
max_lastdate=max(max_lastdate,lastdate);
end;
format min_firstdate max_lastdate date9.;
do until (last.subjid);
set have;
by subjid;
output;
end;
run;
Thanks Arthur. It worked.
Best
Hi Arthur .
The above result is great but how to get only single observation as per distinct subjid ;
Thank in advance .
Consider only the first part of Art's solution
thanks..
Ajay,
Like Fareeza said, just consider the first part. However, add an output statement and you'll probably want to drop some variables. e.g.:
data need (drop=VISITNUM VISIT FIRSTDATE LASTDATE);
do until (last.subjid);
set have;
by subjid;
min_firstdate=min(min_firstdate,firstdate);
max_lastdate=max(max_lastdate,lastdate);
if last.subjid then output;
end;
format min_firstdate max_lastdate date9.;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.