Help using Base SAS procedures

Proc Summary

Accepted Solution Solved
Reply
Super Contributor
Posts: 1,041
Accepted Solution

Proc Summary

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


Accepted Solutions
Solution
‎11-19-2012 03:37 PM
PROC Star
Posts: 7,467

Re: Proc Summary

Posted in reply to robertrao

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=_Smiley Happy;

  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=_Smiley Happy min= max= /autoname;

run;

View solution in original post


All Replies
Frequent Contributor
Posts: 129

Re: Proc Summary

Posted in reply to robertrao

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:-)

Super Contributor
Posts: 1,041

Re: Proc Summary

Posted in reply to LarryWorley

Hi,

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

Thanks

Frequent Contributor
Posts: 129

Re: Proc Summary

Posted in reply to robertrao

Just need to sort by id.


Solution
‎11-19-2012 03:37 PM
PROC Star
Posts: 7,467

Re: Proc Summary

Posted in reply to robertrao

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=_Smiley Happy;

  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=_Smiley Happy min= max= /autoname;

run;

Super Contributor
Posts: 1,041

Re: Proc Summary

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

PROC Star
Posts: 7,467

Re: Proc Summary

Posted in reply to robertrao

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.

Super Contributor
Posts: 1,041

Re: Proc Summary

Thanks a lot for the explanation

Regards

🔒 This topic is solved and locked.

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

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