Help using Base SAS procedures

GET MIN and MAX date datasetp

Accepted Solution Solved
Reply
Contributor
Posts: 34
Accepted Solution

GET MIN and MAX date datasetp

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

SUBJIDVISITNUMVISITFIRSTDATELASTDATE
120BASELINE (RAW)13-May-1014-Jun-10
130MONTH 115-Jun-1012-Aug-10
140MONTH 313-Aug-1011-Nov-10
150MONTH 612-Nov-1012-May-11
170MONTH 1213-May-1110-Nov-11
190MONTH 1811-Nov-118-May-12
1110MONTH 249-May-1213-Nov-12
1130MONTH 3014-Nov-1221-May-13
1150MONTH 3622-May-131-Aug-13

DATA NEED:

SUBJIDVISITNUMVISITFIRSTDATELASTDATEMIN FIRSTDATEMAX LASTDATE
120BASELINE (RAW)13-May-1014-Jun-1013-May-101-Aug-13
130MONTH 115-Jun-1012-Aug-1013-May-101-Aug-13
140MONTH 313-Aug-1011-Nov-1013-May-101-Aug-13
150MONTH 612-Nov-1012-May-1113-May-101-Aug-13
170MONTH 1213-May-1110-Nov-1113-May-101-Aug-13
190MONTH 1811-Nov-118-May-1213-May-101-Aug-13
1110MONTH 249-May-1213-Nov-1213-May-101-Aug-13
1130MONTH 3014-Nov-1221-May-1313-May-101-Aug-13
1150MONTH 3622-May-131-Aug-1313-May-101-Aug-13

OR

SUBJIDMIN FIRSTDATEMAX LASTDATE
113-May-101-Aug-13

Accepted Solutions
Solution
‎02-24-2014 04:03 PM
PROC Star
Posts: 7,471

Re: GET MIN and MAX date datasetp

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;

View solution in original post


All Replies
Super User
Posts: 5,427

Re: GET MIN and MAX date datasetp

Yes you can, but why? SQL is much easier to code.

With the data step use retain.

Data never sleeps
Solution
‎02-24-2014 04:03 PM
PROC Star
Posts: 7,471

Re: GET MIN and MAX date datasetp

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;

Contributor
Posts: 34

Re: GET MIN and MAX date datasetp

Thanks Arthur.  It worked.

Best

Occasional Contributor
Posts: 11

Re: GET MIN and MAX date datasetp

Hi Arthur .

The above result is great but how to get only single observation as per distinct subjid ;

Thank in advance .

Super User
Posts: 19,789

Re: GET MIN and MAX date datasetp

Consider only the first part of Art's solution Smiley Happy

Occasional Contributor
Posts: 11

Re: GET MIN and MAX date datasetp

thanks..

PROC Star
Posts: 7,471

Re: GET MIN and MAX date datasetp

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;

🔒 This topic is solved and locked.

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

Discussion stats
  • 7 replies
  • 442 views
  • 2 likes
  • 5 in conversation