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

## how to compare date

Hello,

I am trying to compare different date. I have two questions here.

1. how to compare those date and conclude as shown in 'want'?

2. the original data is same as shown in 'dataline'. How could I input the date2 as numeric?

thanks

``````dataline
id date1 \$ date2  ;

1 05/21/2010 2013-03-21
2 2011-03-01 2011-01-21
3 2014-03    2011
4 2015-01    2015-05
;
run;

want
1 late
2 early
3 early
4 late``````
1 ACCEPTED SOLUTION

Accepted Solutions
Super User

## Re: how to compare date

The ANYDTDTE informat can read a lot of different date strings. But it doesn't handle a bare 4 digit year. So add a little more logic to handle those cases.

``````data have;
input id date1 date2 @1 id cdate1 :\$10. cdate2 :\$10.;
informat date1 date2 anydtdte. ;
format date1 date2 yymmdd10. ;
if length(cdate1)=4 then date1=mdy(1,1,input(cdate1,4.));
if length(cdate2)=4 then date2=mdy(1,1,input(cdate2,4.));
if date2 < date1 then want='early';
else want='late';
datalines;
1 05/21/2010 2013-03-21
2 2011-03-01 2011-01-21
3 2014-03    2011
4 2015-01    2015-05
;``````
```Obs    id         date1         date2      cdate1      cdate2        want

1      1    2010-05-21    2013-03-21    05/21/2010    2013-03-21    late
2      2    2011-03-01    2011-01-21    2011-03-01    2011-01-21    early
3      3    2014-03-01    2011-01-01    2014-03       2011          early
4      4    2015-01-01    2015-05-01    2015-01       2015-05       late```
6 REPLIES 6
Super User

## Re: how to compare date

The ANYDTDTE informat can read a lot of different date strings. But it doesn't handle a bare 4 digit year. So add a little more logic to handle those cases.

``````data have;
input id date1 date2 @1 id cdate1 :\$10. cdate2 :\$10.;
informat date1 date2 anydtdte. ;
format date1 date2 yymmdd10. ;
if length(cdate1)=4 then date1=mdy(1,1,input(cdate1,4.));
if length(cdate2)=4 then date2=mdy(1,1,input(cdate2,4.));
if date2 < date1 then want='early';
else want='late';
datalines;
1 05/21/2010 2013-03-21
2 2011-03-01 2011-01-21
3 2014-03    2011
4 2015-01    2015-05
;``````
```Obs    id         date1         date2      cdate1      cdate2        want

1      1    2010-05-21    2013-03-21    05/21/2010    2013-03-21    late
2      2    2011-03-01    2011-01-21    2011-03-01    2011-01-21    early
3      3    2014-03-01    2011-01-01    2014-03       2011          early
4      4    2015-01-01    2015-05-01    2015-01       2015-05       late```
Quartz | Level 8

## Re: how to compare date

thanks for reply. if one cdate1 is 2014-03 and another cdate1 is 2014-04, could I convert them to date1 as 2014-03-31 and date1 as 2014-4-30 instead of 01-01?

Opal | Level 21

## Re: how to compare date

@xiangpang wrote:

thanks for reply. if one cdate1 is 2014-03 and another cdate1 is 2014-04, could I convert them to date1 as 2014-03-31 and date1 as 2014-4-30 instead of 01-01?

``````if length(cdate1)=7 then date1 = intnx("month", date1, 0, "END");
if length(cdate2)=7 then date2 = intnx("month", date2, 0, "END"); ``````
PG
Quartz | Level 8

## Re: how to compare date

Thanks a lot. it is what I want.

Amethyst | Level 16

## Re: how to compare date

please try the below code

if there is only year like 2011 then I imputed it to 01-01, let me know if that works for the other imputation suggested is followed.

``````data have;
input id date1 \$10. date2 \$11. ;
if length(date1)<10 then cd1=intnx('month',input(cats(date1,'-01'),yymmdd10.),0,'e');
else if length(date1)>=10 then cd1=input(date1,anydtdte10.);

if length(date2)<=4 then cd2=intnx('year',input(cats(date2,'-01-01'),yymmdd10.),0,'s');
else if 4< length(date2)<10 then cd2=intnx('month',input(cats(date2,'-01'),yymmdd10.),0,'e');
else if length(date2)>=10 then cd2=input(date2,anydtdte10.);
if cd2<cd1 then want='early';
else want='late';
format cd: date9.;
datalines;
1 05/21/2010 2013-03-21
2 2011-03-01 2011-01-21
3 2014-03    2011
4 2015-01    2015-05
;``````
Thanks,
Jag
Quartz | Level 8

## Re: how to compare date

Thanks a lot. I learned intnx function from you guys. I did not know that before.

Have a good day.

Discussion stats
• 6 replies
• 1059 views
• 4 likes
• 4 in conversation