- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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");
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks a lot. it is what I want.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
;
Jag
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks a lot. I learned intnx function from you guys. I did not know that before.
Have a good day.