BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Vivy1
Fluorite | Level 6

Hi all,

 I am trying to calculate month intervals between two dates. Right now, SAS calculates the intervals between October 29, 2019 and November 18,2020 as 12 months although it is indeed over 12 months. I have tried to specify the continuous method in the code but nothing is changing in the results. Any idea what I could be doing wrong?

data outcome;
set Num_a;
admd_n=datepart(admit_dt);
format admd_n yymmdd10.;

dod_n=input(date_of_death,ANYDTDTE21.)*1;
format dod_n yymmdd10.;

months_diff =intck('month', admd_n, dod_n, 'C');
days_diff = intck('day', admd_n, dod_n, 'C');
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

Based on your narrative what defines "within 12 months" I believe you want also to exclude row 4. If so then below should work.

data example;
  format dt1 dt2 comp_dt date9.;
  length sel_flg_desired sel_flg_derived $1;
  infile datalines truncover;
  input dt1:date9. dt2:date9. sel_flg_desired :$1.;

  if month(dt1)=2 and dt1=intnx('month',dt1,0,'e') then comp_dt=intnx('month',dt1,12,'e');
  else comp_dt=intnx('month',dt1,12,'s');

  sel_flg_derived= (dt2<=comp_dt);

  datalines;
29OCT2019 18NOV2020 0
29OCT2019 28OCT2020 1
29OCT2019 29OCT2020 1
29OCT2019 30OCT2020 1
29OCT2019 01NOV2020 0
28FEB2019 27FEB2020 1
28FEB2019 28FEB2020 1
28FEB2019 29FEB2020 1
28FEB2019 01MAR2020 0
29FEB2020 27FEB2021 1
29FEB2020 28FEB2021 1
29FEB2020 01MAR2020 1
28FEB2020 27FEB2021 1
28FEB2020 28FEB2021 1
28FEB2020 01MAR2020 1
05AUG2019 22AUG2020 0
;
run;

proc print data=example;
run;

Patrick_0-1713331717966.png

 

 

 

View solution in original post

7 REPLIES 7
ballardw
Super User

12 months after Oct 29 2019 is Oct 29 2020. Since Nov 18 is not a complete month after Oct 29 you don't get that last increment with the C. So don't use the 'C' if you expect 13 as a result.

 

data example;
   d1 = '29OCT2019'd;
   d2 = '18Nov2020'd;
   m1 = intck('month',d1,d2);
   m2 = intck('month',d1,d2,'C');
run;
Vivy1
Fluorite | Level 6

Thank you. Any idea as to another sas function that will let me exclude this case as within 12 months? I could also try using 365 days

ballardw
Super User

Without a clear description of your "exclusion" rule can't provide code.

You may look at the INTNX function to increment and compare as the 'S' option gives the 'same' (for some meanings of same) day. So when you increment a date like 29OCT for 'month' intervals using 'S' you get the dates 29Nov, 29Dec etc.

So compare the later date to the base date incremented using 'S'.

 

Note, if you have dates of 29Feb you may want to examine any such result closely to see how well that fits your "exclusion" rules.

Vivy1
Fluorite | Level 6
Thank you, removing the 'c' worked. I have another date range between Aug 5 2019 and Aug 22,2020 that m1 is calculating as 12months instead of 13 and specifying the 'c' doesn't make a difference
Patrick
Opal | Level 21

Based on your narrative what defines "within 12 months" I believe you want also to exclude row 4. If so then below should work.

data example;
  format dt1 dt2 comp_dt date9.;
  length sel_flg_desired sel_flg_derived $1;
  infile datalines truncover;
  input dt1:date9. dt2:date9. sel_flg_desired :$1.;

  if month(dt1)=2 and dt1=intnx('month',dt1,0,'e') then comp_dt=intnx('month',dt1,12,'e');
  else comp_dt=intnx('month',dt1,12,'s');

  sel_flg_derived= (dt2<=comp_dt);

  datalines;
29OCT2019 18NOV2020 0
29OCT2019 28OCT2020 1
29OCT2019 29OCT2020 1
29OCT2019 30OCT2020 1
29OCT2019 01NOV2020 0
28FEB2019 27FEB2020 1
28FEB2019 28FEB2020 1
28FEB2019 29FEB2020 1
28FEB2019 01MAR2020 0
29FEB2020 27FEB2021 1
29FEB2020 28FEB2021 1
29FEB2020 01MAR2020 1
28FEB2020 27FEB2021 1
28FEB2020 28FEB2021 1
28FEB2020 01MAR2020 1
05AUG2019 22AUG2020 0
;
run;

proc print data=example;
run;

Patrick_0-1713331717966.png

 

 

 

Patrick
Opal | Level 21

@Vivy1  To better understand the problem and desired outcome which rows from below sample data would you want to select? And why (selection criteria)?

data example;
  input dt1:date9. dt2:date9.;
  m1 = intck('month',dt1,dt2);
  m2 = intck('month',dt1,dt2,'C');
  y1 = intck('year',dt1,dt2);
  y2 = intck('year',dt1,dt2,'C');
  format dt1 dt2 date9.;
  datalines;
29OCT2019 18Nov2020
29OCT2019 28OCT2020
29OCT2019 29OCT2020
29OCT2019 30OCT2020
29OCT2019 01NOV2020
28FEB2019 27FEB2020
28FEB2019 28FEB2020
28FEB2019 29FEB2020
28FEB2019 01MAR2020
29FEB2020 27FEB2021
29FEB2020 28FEB2021
29FEB2020 01MAR2020
28FEB2020 27FEB2021
28FEB2020 28FEB2021
28FEB2020 01MAR2020
;
run;

proc print data=example;
run;

 

Patrick_0-1713314554252.png

 

Vivy1
Fluorite | Level 6

Hi,
I would like to keep all rows except 1,5 and 9. I want all dt2 to be within 12 months of dt1. Looking at your result, perhaps i should not specify the continuous method. I also have dt1 of Aug 5,2019 and dt2 of Aug 22, 2020 that m1 is calculating as 12 months interval instead of 13

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 7 replies
  • 350 views
  • 0 likes
  • 3 in conversation