max date

Occasional Contributor
Posts: 15

max date

Hi i have only character format dates this dates how to calculate maximum dates length also different one data set date length has \$10 another data set length has \$12 in this two data sets i want only max dates .

Ex;

data exp;
length dt \$10;
input id dt \$;
datalines;
101 2017/01/22
101 2018/01/20
102 2015/02/20
;
run;

data exp1;
length dt \$12;
input id dt \$;
datalines;
101 2017/01/25
101 2018/01/30
102 2015/02/19
;
run;

Thank you

Super User
Posts: 9,890

Re: max date

@teja5959 wrote:

Hi i have only character format dates this dates how to calculate maximum dates length also different one data set date length has \$10 another data set length has \$12 in this two data sets i want only max dates .

Ex;

data exp;
length dt \$10;
input id dt \$;
datalines;
101 2017/01/22
101 2018/01/20
102 2015/02/20
;
run;

data exp1;
length dt \$12;
input id dt \$;
datalines;
101 2017/01/25
101 2018/01/30
102 2015/02/19
;
run;

Thank you

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 9,416

Re: max date

Convert to numeric dates.  Get max.  Its will be a lot of effort for no gain to keep them as text.

Super User
Posts: 6,632

Re: max date

[ Edited ]

It's easy enough to run PROC CONTENTS on each data set.  Then you can change the length to the longer length by hard-coding a length:

data exp;

length dt \$ 12;

set exp;

run;

The LENGTH statement must come before the SET statement.  However, you also have to realize that this may not be a good solution.  What if the maximum length turned out to be \$200?  There's probably a lot of extra space being used unnecessarily.  You should at least consider a solution that explores how many characters are needed.  For example:

data test_exp;

set exp;

len_dt = lengthn(dt);

run;

proc means data=test_exp max maxdec=0;

var len_dt;

run;

That program tells you how many characters are actually needed.  You would need to run a similar program for each of your data sets, to discover how many characters are needed across all data sets.

And I agree with @RW9 ... it is worth learning how SAS handles dates, and converting your dates to numeric values.  The solution I provided is more general, and can be applied to non-date variables.

Discussion stats
• 3 replies
• 92 views
• 0 likes
• 4 in conversation