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

Hi Team,

I have two variables as shown.

I want to pick the first and last dates(dates are jumbled) using PROC SUMMARY

To keep thwe ID in a single row.......instead of multiple rows........as min and Max or something like that

Thanks


ID  visit_date(date and time parts present)

1    20mar2012:00:00:00:000

1   27nov2012

1   21jun2012

1   24may2012

1   24may2012

1   24may2012

1   24may2012

1   24may2012

1   12aug2012

1   23sep2012

1  22sep2012

1  25nov2012

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

Unless the following describes what you have, I'm confused by your example data.  Regardless, if you use a class statement, you don't have to presort your data.  e.g.:

data have (drop=_:);

  informat  _visit_date_in anydtdtm22.;

  format visit_date date9.;

  input @;

  _x=length(_infile_);

  if _x gt 15 then _infile_=substr(_infile_,1,_x-4);

  input ID  _visit_date_in;

  visit_date=datepart(_visit_date_in);

  cards;

1 20mar2012:00:00:00:000

1 27nov2012

1 21jun2012

1 24may2012

1 24may2012

1 24may2012

1 24may2012

1 24may2012

1 12aug2012

1 23sep2012

1 22sep2012

1 25nov2012

;

proc summary data=have nway;

  var visit_date;

  class id;

  output out=want (drop=_:) min= max= /autoname;

run;

View solution in original post

7 REPLIES 7
LarryWorley
Fluorite | Level 6

By ID

Variable visit_date

Output the dataset with Only stats - min as first visit and max as last vist.

You may need to add formats to the min and max.

I'll let you figure out the exact syntax:-)

robertrao
Quartz | Level 8

Hi,

Do i need to sort the dataset by ID and date before I do the proc summary

Thanks

LarryWorley
Fluorite | Level 6

Just need to sort by id.


art297
Opal | Level 21

Unless the following describes what you have, I'm confused by your example data.  Regardless, if you use a class statement, you don't have to presort your data.  e.g.:

data have (drop=_:);

  informat  _visit_date_in anydtdtm22.;

  format visit_date date9.;

  input @;

  _x=length(_infile_);

  if _x gt 15 then _infile_=substr(_infile_,1,_x-4);

  input ID  _visit_date_in;

  visit_date=datepart(_visit_date_in);

  cards;

1 20mar2012:00:00:00:000

1 27nov2012

1 21jun2012

1 24may2012

1 24may2012

1 24may2012

1 24may2012

1 24may2012

1 12aug2012

1 23sep2012

1 22sep2012

1 25nov2012

;

proc summary data=have nway;

  var visit_date;

  class id;

  output out=want (drop=_:) min= max= /autoname;

run;

robertrao
Quartz | Level 8

Hi,

This one works perfectly fine for me.

But I have a question.

When I have 2 dates for a particular patient and these dates not sorted and the date variable is numeric

1 22JUN2012

1 22JUL2012

I thought since the first part(datepat) is same it goes to the month part and JUL is less than JUL(alphabetically) so i assumed it will take

the date containing JUL as minimum date and JUN as Maximum date.

Please correct me

Thanks

art297
Opal | Level 21

You said they were dates stored as numbers.  In SAS, datetimes are simply the number of seconds since Jan 1, 1960.

Dates, on the other hand, or just the number of days since Jan 1, 1960.  The datepart function converts a SAS datetime to a SAS date.

Since Jun 22, 2012 is 19,166 days since Jan 1, 1960, and Jul 22, 2012 is 19196 days since Jan 1, 1960, when sorted Jun 22, 2012 will come before (have a lower value) than Jul 22, 2012.

robertrao
Quartz | Level 8

Thanks a lot for the explanation

Regards

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1727 views
  • 6 likes
  • 3 in conversation