BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Almighty
Fluorite | Level 6

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
1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

7 REPLIES 7
LinusH
Tourmaline | Level 20

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

With the data step use retain.

Data never sleeps
art297
Opal | Level 21

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;

Almighty
Fluorite | Level 6

Thanks Arthur.  It worked.

Best

AjayKant
Calcite | Level 5

Hi Arthur .

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

Thank in advance .

Reeza
Super User

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

AjayKant
Calcite | Level 5

thanks..

art297
Opal | Level 21

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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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