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.
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.
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).
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;
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;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.