<?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: Identifying dates for treatment in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Identifying-dates-for-treatment/m-p/930523#M366106</link>
    <description>&lt;P&gt;If there are no overlaps WITHIN sga or WITHIN dm, and both are sorted by ID&amp;nbsp; sga_start/dm_start, then there is a single step solution using conditional SET statements, as in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sga;
input id	(sga_start	sga_end) (:mmddyy10.);
format sga_start	sga_end mmddyy10. ;
cards;
1	1/1/17	7/1/17	 	 
1	10/1/17	12/1/17	 	 
2	2/1/17	4/1/17	 	 
2	9/1/17	11/1/17	
;
data dm;
input id	(dm_start	dm_end) (:mmddyy10.);
format dm_start	dm_end mmddyy10. ;
cards;
1	4/1/17	5/1/17	 	 
1	6/1/17	12/1/17	 	 
2	3/1/17	4/1/17	 	 
2	9/1/17	12/1/17
;


data want (drop=_:);
  merge sga (keep=id sga_start rename=(sga_start=_date) in=insga)
        dm  (keep=id dm_start  rename=(dm_start=_date)  in=indm) ;
  by id _date;


  if insga=1 then set sga ;
  if indm=1 then set dm ;

  if (sga_start &amp;lt;= dm_start &amp;lt;= sga_end)  or
     (sga_start &amp;lt;= dm_end   &amp;lt;= sga_end) then do;
    overlap+1;
    overlap_start=max(dm_start,sga_start);
    overlap_end=min(sga_end,dm_end);
    overlap_length=1 + overlap_end - overlap_start;
    pre_overlap_duration=overlap_start-sga_start;
    format overlap_start overlap_end mmddyy10. ;
    output;
  end;
  if last.id then call missing(of _all_);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to know more about the underlying logic of using conditional SET statements, please see my recent presentation at&amp;nbsp;&lt;A href="https://www.basug.org/videos" target="_blank" rel="noopener"&gt;VIDEOS | BASUG&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edited note:&amp;nbsp; the code above assumes that the [sga_start,sga_end] interval is NEVER within the [dm_start,dm_end] interval, since that is how the sample data was presented.&amp;nbsp; But if it is possible that&amp;nbsp; &amp;nbsp;dm_start &amp;lt;= sga_start &amp;lt;= sga_end &amp;lt;= dm_end, then change&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if (sga_start &amp;lt;= dm_start &amp;lt;= sga_end)  or
     (sga_start &amp;lt;= dm_end   &amp;lt;= sga_end) then do;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;to&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if max(sga_start,dm_start) &amp;lt;= min(sga_end,dm_end) then do;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 05 Jun 2024 01:08:07 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2024-06-05T01:08:07Z</dc:date>
    <item>
      <title>Identifying dates for treatment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-dates-for-treatment/m-p/930290#M366008</link>
      <description>&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Overlapping-drugs/m-p/930264#M365992" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/Overlapping-drugs/m-p/930264#M365992&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;My query is in continuation of the above question which I posted few weeks back, I was unable to continue the same topic as it was closed. How can I know the difference between 'SGA_START' and 'start'? I am trying to merge the dataset 'want' with 'SGA2', how ever I cannot identifying the difference/no. of days between SGA_START and start dates. Can someone please help me with the code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Thu, 30 May 2024 15:30:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-dates-for-treatment/m-p/930290#M366008</guid>
      <dc:creator>stellapersis7</dc:creator>
      <dc:date>2024-05-30T15:30:29Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying dates for treatment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-dates-for-treatment/m-p/930294#M366009</link>
      <description>&lt;P&gt;Marked as having a solution does not "close" a thread on this forum. Though when a question is different enough than a new thread referencing the old one is appropriate.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However that likely means providing a different set of example data sets to discuss. So now you should provide something providing examples of the two (?) sets you expect to merge. You should not expect&amp;nbsp; us to repeatedly rerun code in another thread to get something used in this one. Plus, I am not sure which sets would be needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 May 2024 15:43:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-dates-for-treatment/m-p/930294#M366009</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-05-30T15:43:54Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying dates for treatment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-dates-for-treatment/m-p/930355#M366037</link>
      <description>&lt;P&gt;Could you post the desired output based on your previous example.&lt;/P&gt;
&lt;P&gt;It is hard to guess what you are looking for .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;"&lt;SPAN&gt;difference between 'SGA_START' and 'start'?"&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;You mean then number of days between&amp;nbsp;&lt;SPAN&gt;SGA_START and DM_START ?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sga;
infile cards expandtabs;
input id	(sga_start	sga_end) (:mmddyy10.);
format sga_start	sga_end mmddyy10. ;
cards;
1	1/1/17	7/1/17	 	 
1	10/1/17	12/1/17	 	 
2	2/1/17	4/1/17	 	 
2	9/1/17	11/1/17	
;
data dm;
infile cards expandtabs;
input id	(dm_start	dm_end) (:mmddyy10.);
format dm_start	dm_end mmddyy10. ;
cards;
1	4/1/17	5/1/17	 	 
1	6/1/17	12/1/17	 	 
2	3/1/17	4/1/17	 	 
2	9/1/17	12/1/17
;


data SGA2;
 set sga;
 label='SGA';
 do date=SGA_START to SGA_END;
  output;
 end;
 keep  id date label;
 format date date9.;
run;
data DM2;
 set dm;
 label='DM ';
 do date=DM_START to DM_END;
  output;
 end;
 keep id date label;
 format date date9.;
run;
data temp;
 set SGA2 DM2;
run;
proc sort  data=temp nodupkey;
by id  date label;
run;
data temp2;
do until(last.date);
 set temp;
 by id date;
 length tag $ 40;
 tag=catx('|',tag,label);
end;
drop label;
run;
data temp3;
 set temp2;
 by id tag notsorted;
 if first.tag or dif(date) ne 1 then group+1;
run;
proc sql;
create table want as
select id,group,tag,scan(tag,-1,'|') as flag,min(date) as start format=mmddyy10.,max(date) as end format=mmddyy10.,count(*) as days
 from temp3
  group by id,group,tag
  ;
quit;
data want2;
 set want;
 by id flag notsorted;
 retain sga_start;
 if first.flag then sga_start=ifn(flag='SGA',start,.);
 difference=start-sga_start;  *the variable you want;
 if findc(tag,'|');
 format sga_start mmddyy10.;
 drop flag;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 31 May 2024 01:34:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-dates-for-treatment/m-p/930355#M366037</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-05-31T01:34:01Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying dates for treatment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-dates-for-treatment/m-p/930496#M366098</link>
      <description>&lt;P&gt;So, I want the number of days between SGA_start and &amp;nbsp;the start of overlap period for those patients who had overlap of SGA and DM.&amp;nbsp;&lt;/P&gt;
&lt;TABLE style="width: 606px;" border="0" width="606" cellspacing="0" cellpadding="0"&gt;&lt;COLGROUP&gt;&lt;COL span="5" width="87" /&gt;&lt;COL width="171" /&gt;&lt;/COLGROUP&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="86.78125px" height="57px" class="xl63"&gt;&lt;FONT size="2"&gt;&lt;STRONG&gt;data I have&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.9375px" height="57px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="86.90625px" height="57px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="86.828125px" height="57px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="86.75px" height="57px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="170.84375px" height="57px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="86.78125px" height="30px" class="xl63"&gt;&lt;FONT size="2"&gt;id&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.9375px" height="30px" class="xl63"&gt;&lt;FONT size="2"&gt;sga_start&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.90625px" height="30px" class="xl63"&gt;&lt;FONT size="2"&gt;sga_end&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.828125px" height="30px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="86.75px" height="30px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="170.84375px" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="86.78125px" height="30px" align="right" class="xl63"&gt;&lt;FONT size="2"&gt;1&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.9375px" height="30px" align="right" class="xl64"&gt;&lt;FONT size="2"&gt;1/1/17&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.90625px" height="30px" align="right" class="xl64"&gt;&lt;FONT size="2"&gt;7/1/17&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.828125px" height="30px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="86.75px" height="30px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="170.84375px" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="86.78125px" height="30px" align="right" class="xl63"&gt;&lt;FONT size="2"&gt;1&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.9375px" height="30px" align="right" class="xl64"&gt;&lt;FONT size="2"&gt;10/1/17&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.90625px" height="30px" align="right" class="xl64"&gt;&lt;FONT size="2"&gt;12/1/17&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.828125px" height="30px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="86.75px" height="30px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="170.84375px" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="86.78125px" height="30px" align="right" class="xl63"&gt;&lt;FONT size="2"&gt;2&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.9375px" height="30px" align="right" class="xl64"&gt;&lt;FONT size="2"&gt;2/1/17&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.90625px" height="30px" align="right" class="xl64"&gt;&lt;FONT size="2"&gt;4/1/17&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.828125px" height="30px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="86.75px" height="30px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="170.84375px" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="86.78125px" height="30px" align="right" class="xl63"&gt;&lt;FONT size="2"&gt;2&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.9375px" height="30px" align="right" class="xl64"&gt;&lt;FONT size="2"&gt;9/1/17&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.90625px" height="30px" align="right" class="xl64"&gt;&lt;FONT size="2"&gt;11/1/17&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.828125px" height="30px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="86.75px" height="30px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="170.84375px" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="86.78125px" height="30px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="86.9375px" height="30px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="86.90625px" height="30px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="86.828125px" height="30px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="86.75px" height="30px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="170.84375px" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="86.78125px" height="30px" class="xl63"&gt;&lt;FONT size="2"&gt;id&amp;nbsp;&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.9375px" height="30px" class="xl63"&gt;&lt;FONT size="2"&gt;dm_start&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.90625px" height="30px" class="xl63"&gt;&lt;FONT size="2"&gt;dm_end&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.828125px" height="30px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="86.75px" height="30px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="170.84375px" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="86.78125px" height="30px" align="right" class="xl63"&gt;&lt;FONT size="2"&gt;1&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.9375px" height="30px" align="right" class="xl64"&gt;&lt;FONT size="2"&gt;4/1/17&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.90625px" height="30px" align="right" class="xl64"&gt;&lt;FONT size="2"&gt;5/1/17&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.828125px" height="30px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="86.75px" height="30px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="170.84375px" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="86.78125px" height="30px" align="right" class="xl63"&gt;&lt;FONT size="2"&gt;1&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.9375px" height="30px" align="right" class="xl64"&gt;&lt;FONT size="2"&gt;6/1/17&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.90625px" height="30px" align="right" class="xl64"&gt;&lt;FONT size="2"&gt;12/1/17&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.828125px" height="30px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="86.75px" height="30px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="170.84375px" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="86.78125px" height="30px" align="right" class="xl63"&gt;&lt;FONT size="2"&gt;2&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.9375px" height="30px" align="right" class="xl64"&gt;&lt;FONT size="2"&gt;3/1/17&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.90625px" height="30px" align="right" class="xl64"&gt;&lt;FONT size="2"&gt;4/1/17&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.828125px" height="30px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="86.75px" height="30px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="170.84375px" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="86.78125px" height="30px" align="right" class="xl63"&gt;&lt;FONT size="2"&gt;2&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.9375px" height="30px" align="right" class="xl64"&gt;&lt;FONT size="2"&gt;9/1/17&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.90625px" height="30px" align="right" class="xl64"&gt;&lt;FONT size="2"&gt;12/1/17&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.828125px" height="30px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="86.75px" height="30px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="170.84375px" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="86.78125px" height="30px" class="xl63"&gt;&lt;FONT size="2"&gt;Overlap of SGA and DM&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.9375px" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="86.90625px" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="86.828125px" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="86.75px" height="30px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="170.84375px" height="30px"&gt;&lt;FONT size="2"&gt;&lt;STRONG&gt;data I want&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="86.78125px" height="84px" class="xl63"&gt;&lt;FONT size="2"&gt;id&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.9375px" height="84px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="86.90625px" height="84px" class="xl63"&gt;&lt;FONT size="2"&gt;start&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.828125px" height="84px" class="xl63"&gt;&lt;FONT size="2"&gt;end&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.75px" height="84px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="170.84375px" height="84px" class="xl65"&gt;&lt;FONT size="2"&gt;duration between sga_start and start of overlap&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="86.78125px" height="30px" align="right" class="xl63"&gt;&lt;FONT size="2"&gt;1&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.9375px" height="30px" class="xl63"&gt;&lt;FONT size="2"&gt;overlap1&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.90625px" height="30px" align="right" class="xl64"&gt;&lt;FONT size="2"&gt;4/1/17&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.828125px" height="30px" align="right" class="xl64"&gt;&lt;FONT size="2"&gt;5/1/17&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.75px" height="30px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="170.84375px" height="30px"&gt;&lt;FONT size="2"&gt;92 days&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="86.78125px" height="30px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="86.9375px" height="30px" class="xl63"&gt;&lt;FONT size="2"&gt;overlap2&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.90625px" height="30px" align="right" class="xl64"&gt;&lt;FONT size="2"&gt;6/1/17&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.828125px" height="30px" align="right" class="xl64"&gt;&lt;FONT size="2"&gt;7/1/17&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.75px" height="30px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="170.84375px" height="30px"&gt;&lt;FONT size="2"&gt;153 days&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="86.78125px" height="30px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="86.9375px" height="30px" class="xl63"&gt;&lt;FONT size="2"&gt;overlap3&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.90625px" height="30px" align="right" class="xl64"&gt;&lt;FONT size="2"&gt;10/1/17&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.828125px" height="30px" align="right" class="xl64"&gt;&lt;FONT size="2"&gt;12/1/17&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.75px" height="30px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="170.84375px" height="30px"&gt;&lt;FONT size="2"&gt;0 days&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="86.78125px" height="30px" align="right" class="xl63"&gt;&lt;FONT size="2"&gt;2&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.9375px" height="30px" class="xl63"&gt;&lt;FONT size="2"&gt;overlap1&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.90625px" height="30px" align="right" class="xl64"&gt;&lt;FONT size="2"&gt;3/1/17&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.828125px" height="30px" align="right" class="xl64"&gt;&lt;FONT size="2"&gt;4/1/17&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.75px" height="30px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="170.84375px" height="30px"&gt;&lt;FONT size="2"&gt;28 days&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="86.78125px" height="30px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="86.9375px" height="30px" class="xl63"&gt;&lt;FONT size="2"&gt;overlap2&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.90625px" height="30px" align="right" class="xl64"&gt;&lt;FONT size="2"&gt;9/1/17&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.828125px" height="30px" align="right" class="xl64"&gt;&lt;FONT size="2"&gt;11/1/17&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="86.75px" height="30px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="170.84375px" height="30px"&gt;&lt;FONT size="2"&gt;0 days&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am posting the data sets again for convenience. ( I used the same code you posted before to get the overlap periods)&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;data WORK.SGA;&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;infile datalines dsd truncover;&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;input ENROLID:32. PERIOD:32. SGA_START:MMDDYY10. SGA_END:MMDDYY10.;&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;format SGA_START MMDDYY10. SGA_END MMDDYY10.;&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;label ENROLID="Enrollee ID";&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;datalines;&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;27335102 1 04/25/2017 06/21/2017&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;27335102 2 09/07/2017 12/12/2017&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;27681801 1 01/04/2017 11/21/2018&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;27681801 2 02/13/2019 08/28/2019&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;27681801 3 10/30/2019 12/30/2019&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;;;;;&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;data WORK.DM;&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;infile datalines dsd truncover;&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;input ENROLID:32. PERIOD:32. GLP_START:MMDDYY10. GLP_END:MMDDYY10.;&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;format GLP_START MMDDYY10. GLP_END MMDDYY10.;&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;label ENROLID="Enrollee ID";&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;datalines;&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;27335102 1 06/08/2017 06/08/2017&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;27335102 2 08/12/2017 08/12/2017&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;27335102 3 10/14/2017 10/14/2017&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;27335102 4 12/31/2017 12/31/2017&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;27681801 1 10/31/2017 10/31/2017&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;;;;;&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;data WORK.OVERLAP;&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;infile datalines dsd truncover;&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;input ENROLID:32. group:32. tag:$40. start:MMDDYY10. end:MMDDYY10. days:32.;&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;format start MMDDYY10. end MMDDYY10.;&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;label ENROLID="Enrollee ID";&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;datalines;&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;27335102 2 GLP|SGA 06/08/2017 06/08/2017 1&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;27335102 6 GLP|SGA 10/14/2017 10/14/2017 1&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;27681801 10 GLP|SGA 10/31/2017 10/31/2017 1&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;28593801 15 GLP|SGA 11/02/2018 11/29/2018 28&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;29791002 20 GLP|SGA 05/04/2017 05/04/2017 1&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;;;;;&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;Thank you.&lt;/P&gt;
&lt;P style="font-weight: 400;"&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 01 Jun 2024 00:11:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-dates-for-treatment/m-p/930496#M366098</guid>
      <dc:creator>stellapersis7</dc:creator>
      <dc:date>2024-06-01T00:11:26Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying dates for treatment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-dates-for-treatment/m-p/930502#M366103</link>
      <description>&lt;P&gt;So did you check my code ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1717220522794.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/96897iFFBE78D014BB7C79/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1717220522794.png" alt="Ksharp_0-1717220522794.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 01 Jun 2024 05:42:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-dates-for-treatment/m-p/930502#M366103</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-06-01T05:42:09Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying dates for treatment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-dates-for-treatment/m-p/930523#M366106</link>
      <description>&lt;P&gt;If there are no overlaps WITHIN sga or WITHIN dm, and both are sorted by ID&amp;nbsp; sga_start/dm_start, then there is a single step solution using conditional SET statements, as in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sga;
input id	(sga_start	sga_end) (:mmddyy10.);
format sga_start	sga_end mmddyy10. ;
cards;
1	1/1/17	7/1/17	 	 
1	10/1/17	12/1/17	 	 
2	2/1/17	4/1/17	 	 
2	9/1/17	11/1/17	
;
data dm;
input id	(dm_start	dm_end) (:mmddyy10.);
format dm_start	dm_end mmddyy10. ;
cards;
1	4/1/17	5/1/17	 	 
1	6/1/17	12/1/17	 	 
2	3/1/17	4/1/17	 	 
2	9/1/17	12/1/17
;


data want (drop=_:);
  merge sga (keep=id sga_start rename=(sga_start=_date) in=insga)
        dm  (keep=id dm_start  rename=(dm_start=_date)  in=indm) ;
  by id _date;


  if insga=1 then set sga ;
  if indm=1 then set dm ;

  if (sga_start &amp;lt;= dm_start &amp;lt;= sga_end)  or
     (sga_start &amp;lt;= dm_end   &amp;lt;= sga_end) then do;
    overlap+1;
    overlap_start=max(dm_start,sga_start);
    overlap_end=min(sga_end,dm_end);
    overlap_length=1 + overlap_end - overlap_start;
    pre_overlap_duration=overlap_start-sga_start;
    format overlap_start overlap_end mmddyy10. ;
    output;
  end;
  if last.id then call missing(of _all_);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to know more about the underlying logic of using conditional SET statements, please see my recent presentation at&amp;nbsp;&lt;A href="https://www.basug.org/videos" target="_blank" rel="noopener"&gt;VIDEOS | BASUG&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edited note:&amp;nbsp; the code above assumes that the [sga_start,sga_end] interval is NEVER within the [dm_start,dm_end] interval, since that is how the sample data was presented.&amp;nbsp; But if it is possible that&amp;nbsp; &amp;nbsp;dm_start &amp;lt;= sga_start &amp;lt;= sga_end &amp;lt;= dm_end, then change&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if (sga_start &amp;lt;= dm_start &amp;lt;= sga_end)  or
     (sga_start &amp;lt;= dm_end   &amp;lt;= sga_end) then do;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;to&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if max(sga_start,dm_start) &amp;lt;= min(sga_end,dm_end) then do;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 05 Jun 2024 01:08:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-dates-for-treatment/m-p/930523#M366106</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-06-05T01:08:07Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying dates for treatment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-dates-for-treatment/m-p/930534#M366111</link>
      <description>&lt;P&gt;I did your previous code and got the dataset overlap. I merged back with dataset- SGA to get SGA_Start, and did (duration = start-SGA_Start). However, there are multiple durations for each 'start' because of multiple SGA_Start. For example, please check the screenshot below. for observations, 31-36, there is a same start and end dates which calculated duration against many SGA_Start dates. However, I want the difference with the nearest SGA_start date only. So for the ones between 31-36, I will need to capture 34 only where the duration is captured to the nearest SGA_Start date.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screen Shot 2024-06-01 at 11.39.42.png" style="width: 767px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/96911iC00CE9980602AE92/image-dimensions/767x115?v=v2" width="767" height="115" role="button" title="Screen Shot 2024-06-01 at 11.39.42.png" alt="Screen Shot 2024-06-01 at 11.39.42.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screen Shot 2024-06-01 at 11.53.40.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/96912iF8496C48824EF3DD/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Screen Shot 2024-06-01 at 11.53.40.png" alt="Screen Shot 2024-06-01 at 11.53.40.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt; &lt;/P&gt;
&lt;P&gt; &lt;/P&gt;</description>
      <pubDate>Sat, 01 Jun 2024 16:55:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-dates-for-treatment/m-p/930534#M366111</guid>
      <dc:creator>stellapersis7</dc:creator>
      <dc:date>2024-06-01T16:55:11Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying dates for treatment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-dates-for-treatment/m-p/930554#M366122</link>
      <description>So you did not use the code I posted and use MERGE statement by yourself ?&lt;BR /&gt;Try running my code and open dataset WANT2 and check variable DIFFERENCE .&lt;BR /&gt;use the data in my code and explain where is wrong .</description>
      <pubDate>Sun, 02 Jun 2024 00:36:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-dates-for-treatment/m-p/930554#M366122</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-06-02T00:36:48Z</dc:date>
    </item>
  </channel>
</rss>

