<?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 comparing dates across columns proc sql in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/comparing-dates-across-columns-proc-sql/m-p/355098#M83166</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I was wondering if I could get your help with modifying the PROC SQL code, marked as solved here:&lt;/P&gt;&lt;P&gt;&lt;A href="https://communities.sas.com/t5/Base-SAS-Programming/Concomitant-medication-use/m-p/355097#M83165" target="_blank"&gt;https://communities.sas.com/t5/Base-SAS-Programming/Concomitant-medication-use/m-p/355097#M83165&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;so that it outputs 2 additional columns containing the 2 dates that were used to compute the variable 'overlap' days. I can do it for two drug combinations in a simple separate data step, but I found that the code would get quite hairy when comparing across start and end dates for 3-5 different drugs. Any help would be much appreciated!&amp;nbsp;Thank you very much!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 01 May 2017 22:34:27 GMT</pubDate>
    <dc:creator>m_o</dc:creator>
    <dc:date>2017-05-01T22:34:27Z</dc:date>
    <item>
      <title>comparing dates across columns proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/comparing-dates-across-columns-proc-sql/m-p/355098#M83166</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I was wondering if I could get your help with modifying the PROC SQL code, marked as solved here:&lt;/P&gt;&lt;P&gt;&lt;A href="https://communities.sas.com/t5/Base-SAS-Programming/Concomitant-medication-use/m-p/355097#M83165" target="_blank"&gt;https://communities.sas.com/t5/Base-SAS-Programming/Concomitant-medication-use/m-p/355097#M83165&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;so that it outputs 2 additional columns containing the 2 dates that were used to compute the variable 'overlap' days. I can do it for two drug combinations in a simple separate data step, but I found that the code would get quite hairy when comparing across start and end dates for 3-5 different drugs. Any help would be much appreciated!&amp;nbsp;Thank you very much!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 01 May 2017 22:34:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/comparing-dates-across-columns-proc-sql/m-p/355098#M83166</guid>
      <dc:creator>m_o</dc:creator>
      <dc:date>2017-05-01T22:34:27Z</dc:date>
    </item>
    <item>
      <title>Re: comparing dates across columns proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/comparing-dates-across-columns-proc-sql/m-p/355107#M83167</link>
      <description>&lt;P&gt;Aren't those the dates already included in your current code? I had to adjust the input statement so that it read the dates correctly:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
  infile cards dlm=',' dsd;
  informat START_DT MMDDYY8.;
  informat END_DT MMDDYY8.;
  input ID DRUG $ START_DT DAYS_SUPP  END_DT;
  FORMAT START_DT END_DT MMDDYY10.;
cards; 
1,A,2/17/10,30,3/19/10
1,B,5/6/09,30,6/5/09
1,C,7/9/11,60,9/7/11
1,E,3/1/10,90,5/30/10
1,B,1/1/09,90,4/1/09
1,D,2/1/09,30,3/3/09
1,C,5/6/12,90,8/4/12
2,B,4/1/12,60,5/31/12
2,A,7/1/10,30,7/31/10
2,C,8/3/10,90,11/1/10
2,D,11/1/13,90,1/30/14
2,E,12/5/13,90,3/5/14
2,A,2/1/11,90,5/2/11
;
RUN;

PROC SQL;
  CREATE TABLE HAVE AS
    SELECT MONOTONIC() AS ROWID, * FROM HAVE
  ;
QUIT;

%let fromDate = 01Jan2000;
%let toDate = 31DEC2016;
data alldates;
  length key 8; 
  do date = "&amp;amp;fromDate"d to "&amp;amp;toDate"d; 
    key + 1; 
    RSA_WorkdayInd = ( 2 &amp;lt;= weekday(date) &amp;lt;= 6);
    WeekendInd = (not RSA_WorkdayInd);
    Calendar_Week_Number = week(date, "V");
    DayOfWeek = put(date, downame3.);
    Fin_Year = year(intnx("year.7", date, 0));
    output; 
  end; 

  format 
    date date9.
  ;
run; 

PROC SQL;
  CREATE TABLE OVERLAP AS
    SELECT HAVE.*, HAVE2.ROWID AS ROWID2, HAVE2.ID AS ID2, 
     HAVE2.START_DT AS START2, HAVE2.DAYS_SUPP AS DAYS2, 
     HAVE2.END_DT AS END2, 
     (SELECT COUNT(*) FROM alldates
       WHERE DATE &amp;gt;= HAVE.START_DT AND DATE &amp;gt;= HAVE2.START_DT AND
             DATE &amp;lt;= HAVE.END_DT AND DATE &amp;lt;= HAVE2.END_DT) AS OVERLAP
         FROM HAVE, HAVE AS HAVE2
           WHERE HAVE.ROWID NE HAVE2.ROWID
               AND HAVE.ID = HAVE2.ID
               AND (HAVE.START_DT &amp;lt;= HAVE2.END_DT)
               and (HAVE2.START_DT &amp;lt;= HAVE.END_DT)
  ;
QUIT;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 May 2017 00:04:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/comparing-dates-across-columns-proc-sql/m-p/355107#M83167</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-05-02T00:04:33Z</dc:date>
    </item>
    <item>
      <title>Re: comparing dates across columns proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/comparing-dates-across-columns-proc-sql/m-p/355113#M83170</link>
      <description>&lt;P&gt;Thank you very much for your response. It is just that in some cases, the 'overlap' column is the difference between have.start_dt and have2.end_t, or between have2.start_dt and have.end_dt, or have.start_dt and have.end_dt or have2.start_dt and have2.end_dt. To illustrate:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;overlap is the difference between dates a and b&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;a. _______ b.&lt;/P&gt;&lt;P&gt;c.______________d.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;or&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;overlap is the difference between dates c and d&lt;/P&gt;&lt;P&gt;a.__________________b.&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;c.________d.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;or&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;overlap is the difference between dates c and b&lt;/P&gt;&lt;P&gt;a.____________b.&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;c._______________d.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;or&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;overlap is the difference between dates a and d&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a.___________b.&lt;/P&gt;&lt;P&gt;c.____________d.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was wondering if there is a way to output the&amp;nbsp;two dates that were&amp;nbsp;used to compute the overlapping days. Thank you very much for any help you could give!&lt;/P&gt;</description>
      <pubDate>Tue, 02 May 2017 03:53:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/comparing-dates-across-columns-proc-sql/m-p/355113#M83170</guid>
      <dc:creator>m_o</dc:creator>
      <dc:date>2017-05-02T03:53:49Z</dc:date>
    </item>
    <item>
      <title>Re: comparing dates across columns proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/comparing-dates-across-columns-proc-sql/m-p/355125#M83174</link>
      <description>&lt;P&gt;Does the following do what you want?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;
  set overlap;
  array dif(4) ab cd cb ad;
  dif(1)=end_dt-start_dt+1;
  dif(2)=end2-start2+1;
  dif(3)=end_dt-start2+1;
  dif(4)=end2-start_dt+1;
  x=vname(dif(whichn(overlap,of dif(*))));
run;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and, if so, you could also&amp;nbsp;get the same result by adding one more step to your proc sql code, namely:&lt;/P&gt;
&lt;PRE&gt;PROC SQL;
  CREATE TABLE OVERLAP AS
    SELECT HAVE.*, HAVE2.ROWID AS ROWID2, HAVE2.ID AS ID2, 
     HAVE2.START_DT AS START2, HAVE2.DAYS_SUPP AS DAYS2, 
     HAVE2.END_DT AS END2, 
     (SELECT COUNT(*) FROM alldates
       WHERE DATE &amp;gt;= HAVE.START_DT AND DATE &amp;gt;= HAVE2.START_DT AND
             DATE &amp;lt;= HAVE.END_DT AND DATE &amp;lt;= HAVE2.END_DT) AS OVERLAP
         FROM HAVE, HAVE AS HAVE2
           WHERE HAVE.ROWID NE HAVE2.ROWID
               AND HAVE.ID = HAVE2.ID
               AND (HAVE.START_DT &amp;lt;= HAVE2.END_DT)
               and (HAVE2.START_DT &amp;lt;= HAVE.END_DT)
  ;
  create table want as
    select *,
     case
       when end_dt-start_dt+1=overlap then 'ab'
       when end2-start2+1=overlap then 'cd'
       when end_dt-start2+1=overlap then 'cb'
       when end2-start_dt+1=overlap then 'ad'
       else 'uk'
     end as result
     from overlap
  ;
QUIT;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 May 2017 04:44:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/comparing-dates-across-columns-proc-sql/m-p/355125#M83174</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-05-02T04:44:22Z</dc:date>
    </item>
    <item>
      <title>Re: comparing dates across columns proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/comparing-dates-across-columns-proc-sql/m-p/355128#M83175</link>
      <description>&lt;P&gt;So the dates used are MAX(a, c) and MIN(b, d)&lt;/P&gt;</description>
      <pubDate>Tue, 02 May 2017 04:39:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/comparing-dates-across-columns-proc-sql/m-p/355128#M83175</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2017-05-02T04:39:29Z</dc:date>
    </item>
    <item>
      <title>Re: comparing dates across columns proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/comparing-dates-across-columns-proc-sql/m-p/355400#M83235</link>
      <description>&lt;P&gt;Thank you so much Art for your help! I am trying to extract the actual dates that were used to compute 'overlap' so that I would have two more columns containing the overlap start date and the overlap end date. The post by PGStats seems to be working. Thank you again for your help! Appreciate it! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 02 May 2017 21:14:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/comparing-dates-across-columns-proc-sql/m-p/355400#M83235</guid>
      <dc:creator>m_o</dc:creator>
      <dc:date>2017-05-02T21:14:53Z</dc:date>
    </item>
    <item>
      <title>Re: comparing dates across columns proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/comparing-dates-across-columns-proc-sql/m-p/355402#M83237</link>
      <description>&lt;P&gt;Thank you very much for your response. I think your code seems to be exactly what I need. Curiously, the 'overlap' variable calculated using the long sql code above agrees with your code (i.e. the difference between the two dates identified using min and max) agrees about 99% of the time but not a 100%. I may have to troubleshoot why that's happening but your code seems to be more accurate for calculating 'overlap' &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; Thank you again!&lt;/P&gt;</description>
      <pubDate>Tue, 02 May 2017 21:12:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/comparing-dates-across-columns-proc-sql/m-p/355402#M83237</guid>
      <dc:creator>m_o</dc:creator>
      <dc:date>2017-05-02T21:12:21Z</dc:date>
    </item>
  </channel>
</rss>

