<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Need to Calculate Days Between All Possible Combinations of 6 Date Variables &amp;amp; Find Matching in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Need-to-Calculate-Days-Between-All-Possible-Combinations-of-6/m-p/793541#M254362</link>
    <description>Sorry but that want dataset is not the one I described. The dates are also not correct in your want dataset. I figured out a solution myself that involves nested loops. Thank you for your assistance!</description>
    <pubDate>Mon, 31 Jan 2022 16:07:47 GMT</pubDate>
    <dc:creator>cgates</dc:creator>
    <dc:date>2022-01-31T16:07:47Z</dc:date>
    <item>
      <title>Need to Calculate Days Between All Possible Combinations of 6 Date Variables &amp; Find Matching Set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-Calculate-Days-Between-All-Possible-Combinations-of-6/m-p/793175#M254181</link>
      <description>&lt;P&gt;I have a wide dataset with 6 dates and 6 types. Each type number corresponds to the date number. I need to compare each date and type to each other (2 at a time) and find the earliest date that these requirements are met:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Criteria&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;If both types = P, the difference between the dates must be 17 or more.&lt;/P&gt;
&lt;P&gt;All other combinations must be 24 or more days apart&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have to find the earliest date match. So in my example, my "have" dataset has all 6 entries. My want dataset has the earliest two dates that meet my criteria which are 2 &amp;amp; 4. I can calculate this manually but can't figure out how to do it in SAS. I would love to have this in some sort of iterative macro program instead of taking up hundreds of lines of code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;My logic on paper&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;1. Calculate # of days between each admin_date (all permutations)&lt;BR /&gt;2. Ignore any that are not at least 17 days apart&lt;BR /&gt;3. Among those that are &amp;gt;= 17 and &amp;lt;24, check to see if both types are P. If so, take the combination with the earliest 2nd date.&lt;BR /&gt;4. If not, check those that are &amp;gt;= 24. Take the combination with the earliest 2nd date.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&lt;STRONG&gt;My calculations for Day Differences Between:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;1 &amp;amp; 2: 1 day (ignore, less than 17)&lt;/P&gt;
&lt;P&gt;1 &amp;amp; 3: 7 days (ignore, less than 17)&lt;/P&gt;
&lt;P&gt;1 &amp;amp; 4: 18 days (ignore, not both P)&lt;/P&gt;
&lt;P&gt;1 &amp;amp; 5: 20 days (ignore, not both P)&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;1 &amp;amp; 6: 34 days (consider, ge 24)&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;2 &amp;amp; 3: 6 days (ignore, less than 17)&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;2 &amp;amp; 4: 17 days (consider, ge 17, both P)&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;2 &amp;amp; 5: 19 days (consider, ge 17, both P)&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;2 &amp;amp; 6: 33 days (consider, ge 24)&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;and so forth&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Among all of the sets that met the criteria: 1 and 6, 2 and 4, 2 and 5, 2 and 6. 4 is the earliest so that's what I want.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
input person $
      admin_date1 : ?? mmddyy10.
      admin_date2 : ??mmddyy10. 
      admin_date3 : ?? mmddyy10.
      admin_date4 : ?? mmddyy10.
      admin_date5 : ?? mmddyy10. 
      admin_date6 : ?? mmddyy10.
      type1 $      
      type2 $      
      type3 $     
      type4 $   
      type5 $   
      type6 $;
format admin_date1 mmddyy10.
      admin_date2 mmddyy10. 
      admin_date3 mmddyy10.
      admin_date4 mmddyy10.
      admin_date5 mmddyy10. 
      admin_date6 mmddyy10.;
datalines;
JohnDoe 01/12/2021 01/13/2021 01/19/2021 01/30/2021 02/01/2021 02/15/2021 M P M P P J
;
run; 



data want;
input person $
      admin_date1 : ?? mmddyy10.
      admin_date2 : ??mmddyy10. 
      type1 $      
      type2 $      
;
format admin_date1 mmddyy10.
      admin_date2 mmddyy10. 
;
datalines;
JohnDoe 01/13/2021 01/30/2021 P P
;
run; &lt;/PRE&gt;</description>
      <pubDate>Fri, 28 Jan 2022 18:22:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-Calculate-Days-Between-All-Possible-Combinations-of-6/m-p/793175#M254181</guid>
      <dc:creator>cgates</dc:creator>
      <dc:date>2022-01-28T18:22:00Z</dc:date>
    </item>
    <item>
      <title>Re: Need to Calculate Days Between All Possible Combinations of 6 Date Variables &amp; Find Matching</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-Calculate-Days-Between-All-Possible-Combinations-of-6/m-p/793370#M254281</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test1;
    set have;
    array a admin_date1-admin_date6;
    array b admin_date1-admin_date6  ;
    array x $ type1-type6;
    array y $ type1-type6;
    array v(5,6) $10 a1b1-a1b6 a2b1-a2b6  a3b1-a3b6  a4b1-a4b6 a5b1-a5b6;
    do i=1 to 5;
        do j=i+1 to 6;
        /*  case 1:  If both types = P, the difference between the dates must be 17 or more.
            case 2:  All other combinations must be 24 or more days apart*/
            if (x(i)='P' and y(j)='P' and b(j)-a(i)&amp;gt;=17)
                or ((x(i)^='P' or y(j)^='P') and b(j)-a(i)&amp;gt;=24) then 
             do;
                v(i,j)='Y'; output;
             end;    
        end;
    end;
run;

data want;
    set test1;
    format admin_date_1-admin_date_6 mmddyy10.;
    array a admin_date1-admin_date6;
    array b admin_date_1-admin_date_6 ;
    array x $ type1-type6;
    array y $ type_1-type_6;
    array v(5,6) $10 a1b1-a1b6 a2b1-a2b6  a3b1-a3b6  a4b1-a4b6 a5b1-a5b6;
    do n=1 to 5;
        do m=n+1 to 6;
            if  v(n,m)='Y' then do; b(n)=a(n); y(m)=x(m); end;
        end;
    end;
    keep person i j admin_date_1-admin_date_6  type_1-type_6;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 30 Jan 2022 14:40:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-Calculate-Days-Between-All-Possible-Combinations-of-6/m-p/793370#M254281</guid>
      <dc:creator>blueskyxyz</dc:creator>
      <dc:date>2022-01-30T14:40:01Z</dc:date>
    </item>
    <item>
      <title>Re: Need to Calculate Days Between All Possible Combinations of 6 Date Variables &amp; Find Matching</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-Calculate-Days-Between-All-Possible-Combinations-of-6/m-p/793541#M254362</link>
      <description>Sorry but that want dataset is not the one I described. The dates are also not correct in your want dataset. I figured out a solution myself that involves nested loops. Thank you for your assistance!</description>
      <pubDate>Mon, 31 Jan 2022 16:07:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-Calculate-Days-Between-All-Possible-Combinations-of-6/m-p/793541#M254362</guid>
      <dc:creator>cgates</dc:creator>
      <dc:date>2022-01-31T16:07:47Z</dc:date>
    </item>
    <item>
      <title>Re: Need to Calculate Days Between All Possible Combinations of 6 Date Variables &amp; Find Matching</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-Calculate-Days-Between-All-Possible-Combinations-of-6/m-p/793545#M254363</link>
      <description>&lt;P&gt;This is clunky but what I figured out on my own and works.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;&lt;BR /&gt;input person $&lt;BR /&gt;admin_date_1 : ?? mmddyy10.&lt;BR /&gt;admin_date_2 : ??mmddyy10. &lt;BR /&gt;admin_date_3 : ?? mmddyy10.&lt;BR /&gt;admin_date_4 : ?? mmddyy10.&lt;BR /&gt;admin_date_5 : ?? mmddyy10. &lt;BR /&gt;admin_date_6 : ?? mmddyy10.&lt;BR /&gt;type_1 $ &lt;BR /&gt;type_2 $ &lt;BR /&gt;type_3 $ &lt;BR /&gt;type_4 $ &lt;BR /&gt;type_5 $ &lt;BR /&gt;type_6 $;&lt;BR /&gt;format admin_date_1 mmddyy10.&lt;BR /&gt;admin_date_2 mmddyy10. &lt;BR /&gt;admin_date_3 mmddyy10.&lt;BR /&gt;admin_date_4 mmddyy10.&lt;BR /&gt;admin_date_5 mmddyy10. &lt;BR /&gt;admin_date_6 mmddyy10.;&lt;BR /&gt;datalines;&lt;BR /&gt;JohnDoe 01/12/2021 01/13/2021 01/19/2021 01/30/2021 02/01/2021 02/15/2021 M P M P P J&lt;BR /&gt;;&lt;BR /&gt;run; &lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;*in order for this to work correctly, you have to order the DAYS_BT_DOSE fields in ascending order like below in the format;
%macro test;

data want (keep=person new:);
set have;

format L_DAYS_BT_DOSE_1_2	L_DAYS_BT_DOSE_1_3	L_DAYS_BT_DOSE_2_3	L_DAYS_BT_DOSE_1_4	L_DAYS_BT_DOSE_2_4
       L_DAYS_BT_DOSE_3_4           L_DAYS_BT_DOSE_1_5	L_DAYS_BT_DOSE_2_5	L_DAYS_BT_DOSE_3_5	L_DAYS_BT_DOSE_4_5	L_DAYS_BT_DOSE_1_6	L_DAYS_BT_DOSE_2_6	
       L_DAYS_BT_DOSE_3_6	        L_DAYS_BT_DOSE_4_6	L_DAYS_BT_DOSE_5_6   

       F_DAYS_BT_DOSE_1_2	        F_DAYS_BT_DOSE_1_3	F_DAYS_BT_DOSE_2_3	F_DAYS_BT_DOSE_1_4	F_DAYS_BT_DOSE_2_4
       F_DAYS_BT_DOSE_3_4           F_DAYS_BT_DOSE_1_5	F_DAYS_BT_DOSE_2_5	F_DAYS_BT_DOSE_3_5	F_DAYS_BT_DOSE_4_5	F_DAYS_BT_DOSE_1_6	F_DAYS_BT_DOSE_2_6	
       F_DAYS_BT_DOSE_3_6	        F_DAYS_BT_DOSE_4_6	F_DAYS_BT_DOSE_5_6   NEW_ADMIN_DT_1 NEW_ADMIN_DT_2 MMDDYY10. 
	   ;


%do i = 1 %to 5; 

%let nxt = %eval(&amp;amp;i. + 1); 

	%do b=&amp;amp;nxt %to 6; 
 
		if TYPE_&amp;amp;i = 'P' and TYPE_&amp;amp;b = 'P' and intck('day',ADMIN_DATE_&amp;amp;i,ADMIN_DATE_&amp;amp;b) ge 17
        	then do;
				L_DAYS_BT_DOSE_&amp;amp;i._&amp;amp;b = ADMIN_DATE_&amp;amp;b; 
				F_DAYS_BT_DOSE_&amp;amp;i._&amp;amp;b = ADMIN_DATE_&amp;amp;i; 
			end;
		else if TYPE_&amp;amp;i in ('P','M','J') and TYPE_&amp;amp;b in ('P','M','J') and intck('day',ADMIN_DATE_&amp;amp;i,ADMIN_DATE_&amp;amp;b) ge 24
        	then do;
				L_DAYS_BT_DOSE_&amp;amp;i._&amp;amp;b = ADMIN_DATE_&amp;amp;b; 
				F_DAYS_BT_DOSE_&amp;amp;i._&amp;amp;b = ADMIN_DATE_&amp;amp;i; 
			end;

	%end;
%end;

NEW_ADMIN_DT_1 = COALESCE(of F_DAYS_BT_DOSE:);
NEW_ADMIN_DT_2 = COALESCE(of L_DAYS_BT_DOSE:);


run;

%mend test;


*execute macro;
%test;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 31 Jan 2022 16:21:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-Calculate-Days-Between-All-Possible-Combinations-of-6/m-p/793545#M254363</guid>
      <dc:creator>cgates</dc:creator>
      <dc:date>2022-01-31T16:21:18Z</dc:date>
    </item>
  </channel>
</rss>

