BookmarkSubscribeRSS Feed
Sharan
Obsidian | Level 7
I have a series of dates in the dates column in the format 01jan2005.
How do I find the first date and last date? Please
7 REPLIES 7
Reeza
Super User

It would help immensely if you provide sample data, expected output and anything you've tried. 

 

What is your definition of first and last? 

If you cannot provide your actual data for some reason, please make fake data that represents your problem. 

If you need a data set with dates as an example, use sashelp.stocks which has stock data over time. 

 

So if this was your data set what do you want as output. 

 

data IBM;
set stocks;
where stock='IBM';
keep date;
run;


@Sharan wrote:
I have a series of dates in the dates column in the format 01jan2005.
How do I find the first date and last date? Please

Generic questions otherwise, get a generic answer. 

Assuming first and last actually mean minimum and maximum dates, use proc means to get the min/max of the column and you have the first and last date. 

Sharan
Obsidian | Level 7
My data set looks like below:

Dispense date of antibiotic

03Sep2010
01Jan2005
30Jan2006
06 Apr2008
01Jan2009
30Jan2018
06 Apr2018
01Jan2015
30Jan2006
06 Apr2018
01Jan2005
30Jan2006
16 Apr2008
18Dec2020
05Jun2019

so here the earliest date is 01Jan2005
and the latest date is 18Dec2020

So please explain how i can obtain the two dates.
Thank you!




yabwon
Onyx | Level 15
data have;
input date date9.;
format date date9.;
cards;
03Sep2010
01Jan2005
30Jan2006
06Apr2008
01Jan2009
30Jan2018
06Apr2018
01Jan2015
30Jan2006
06Apr2018
01Jan2005
30Jan2006
16Apr2008
18Dec2020
05Jun2019
;
run;
proc print;
run;

/* first way O(n) */
data want1;
  set have end=end;
  first = min(first, date);
  last  = max(last,  date);
  put _all_;
  retain first last;

  if end then output;
  format first last date9.;
run;
proc print;
run;


/* second way ~O(nlogn) [assuming "good" quicksort] :-) */
proc sort data = have out = want2;
 by date;
run;
data want2;
  point = 1;
  set want2 point = point nobs=nobs;
  first = date;
  set want2 point = nobs;
  last = date;
  output;
  stop;
  format first last date9.;
run;
proc print;
run;

B.

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Sharan
Obsidian | Level 7
My data set looks like below:
 
Dispense date of antibiotic
 
03Sep2010
01Jan2005
30Jan2006
06 Apr2008
01Jan2009
30Jan2018
06 Apr2018
01Jan2015
30Jan2006
06 Apr2018
01Jan2005
30Jan2006
16 Apr2008
18Dec2020
05Jun2019
 
so here the earliest date is 01Jan2005
and the latest date is 18Dec2020 
 
So please explain how I can obtain the two dates.
Thank you!
ballardw
Super User

Do you want report? If so, what would the report look like?

If you want a data set, what does that look like?

For the entire set? Or some sort of subset like each person/location/activity?

 

Note: Appearance, which is all a format is, has nothing to do with comparisons that might be used such earliest (minimum numeric value) or latest (maximum numeric value).

Kurt_Bremser
Super User

If these are SAS dates and not just strings that look like dates, then this will get your values:

proc sql;
select
  min(date) as first format=yymmdd10.,
  max(date) as last format=yymmdd10.
from have;
quit;
andreas_lds
Jade | Level 19

You are working with datasets that has only one variable? Interesting.

You could use proc summary/means:

proc summary data=have;
   var date;
   output out=firstlast(drop= _:) min=first_date max=last_date;
run;

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 2121 views
  • 1 like
  • 6 in conversation