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

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
Tom
Super User Tom
Super User

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

View solution in original post

6 REPLIES 6
Tom
Super User Tom
Super User

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
xiangpang
Quartz | Level 8

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?

PGStats
Opal | Level 21

@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?


Add the statements

 

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

Thanks a lot. it is what I want.

Jagadishkatam
Amethyst | Level 16

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
xiangpang
Quartz | Level 8

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

 

Have a good day.

 

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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