<?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 Eliminate Non-Overlapping Terms in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Eliminate-Non-Overlapping-Terms/m-p/426980#M105258</link>
    <description>&lt;P&gt;I have a list of adverse events (AEs), and I am supposed to find if a subject has any overlapping terms and output them into a report.&amp;nbsp; I have been able to do so, but on rare occasions, I have a term that is not overlapping in some places but is in others and is outputting all records.&amp;nbsp; I would appreciate some help finding a way to eliminate the records that are not truly overlapping.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example,&amp;nbsp;Subject 101-002 is showing that "Urinary tract infection" is overlapping, which is true for AESPID 2 &amp;amp; 4; however, AESPID=1 is not overlapping.&amp;nbsp; I want to get rid of this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;SUBJECTNUMBERSTR&lt;/TD&gt;&lt;TD&gt;AESPID&lt;/TD&gt;&lt;TD&gt;LLT_NAME&lt;/TD&gt;&lt;TD&gt;AESTDAT&lt;/TD&gt;&lt;TD&gt;AEENDAT&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;101-002&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;Urinary tract infection&lt;/TD&gt;&lt;TD&gt;03AUG2017&lt;/TD&gt;&lt;TD&gt;15AUG2017&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;SPAN&gt;101-00&lt;/SPAN&gt;&lt;SPAN&gt;2&lt;/SPAN&gt;&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;Urinary tract infection&lt;/TD&gt;&lt;TD&gt;07SEP2017&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;SPAN&gt;101-00&lt;/SPAN&gt;&lt;SPAN&gt;2&lt;/SPAN&gt;&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;Urinary tract infection&lt;/TD&gt;&lt;TD&gt;03OCT2017&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;My method for getting to this point is:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Get a list of all Subject/LLT records where there is a repeat of&amp;nbsp;LLT_NAME&lt;/LI&gt;&lt;LI&gt;Compare all AESTDAT &amp;amp; AEENDAT values to see if there is an overlap and flag&lt;/LI&gt;&lt;LI&gt;Keep distinct Subject/LLT records with a flag&lt;/LI&gt;&lt;LI&gt;Merge the overlapping records with the original data and only keep overlapping terms&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&amp;nbsp;Here is the code I have.&amp;nbsp; Please feel free to modify to help me remove AESPID=1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data AEALL;
	input @1 SUBJECTNUMBERSTR $7. @9 AESPID @13 LLT_NAME $30. @45 AESTDAT date9. @57 AEENDAT date9.;
	format AESTDAT AEENDAT date9.;
	cards;
101-001	1	Acute respiratory failure		05AUG2017	.
101-001	2	Pleural effusion				05AUG2017	.
101-001	3	Pneumonia						07AUG2017	14AUG2017
101-001	4	Septic shock					16AUG2017	22AUG2017
101-001	5	Pneumothorax traumatic			19AUG2017	06SEP2017
101-001	6	Anemia							20AUG2017	.
101-001	7	Pneumothorax traumatic			28AUG2017	09SEP2017
101-001	8	Pneumonia						29AUG2017	08SEP2017
101-001	9	Hypotension						04SEP2017	07SEP2017
101-001	10	Hypernatremia					12AUG2017	23AUG2017
101-001	11	Thrombocytosis					15AUG2017	20AUG2017
101-001	12	Paroxysmal atrial fibrillation	13SEP2017	.
101-001	13	Respiratory infection			15SEP2017	.
101-002	1	Urinary tract infection			03AUG2017	15AUG2017
101-002	2	Urinary tract infection			07SEP2017	.
101-002	3	Pressure ulcer					11SEP2017	.
101-002	4	Urinary tract infection			03OCT2017	.
;
run;


proc sort data=aeall;
	by subjectnumberstr llt_name aestdat aeendat;
	where llt_name^='';
run;

data duplicate;
	set aeall;
	by subjectnumberstr llt_name;
	if first.llt_name &amp;amp; last.llt_name then delete;
run;

proc sort data=duplicate;
	by subjectnumberstr llt_name aespid;
run;


data overlap;
	set duplicate;
	by subjectnumberstr llt_name aespid;
	format prev_stdat prev_endat date9.;
	prev_stdat=lag(aestdat);
	prev_endat=lag(aeendat);
	if first.llt_name then do;
		prev_stdat=.;
		prev_endat=.;
	end;
	else do;
		if (prev_endat^=. &amp;amp; aestdat^=. &amp;amp; prev_stdat&amp;lt;=aestdat &amp;amp; aestdat&amp;lt;=prev_endat) or
			(prev_endat=. &amp;amp; aestdat^=. &amp;amp; prev_stdat&amp;lt;=aestdat) or
			(prev_endat^=. &amp;amp; aestdat^=. &amp;amp; aestdat=prev_endat+1) or
			(aestdat=.) then flag=1;
	end;
run;

proc sort data=overlap out=overlap_distinct (keep=subjectnumberstr llt_name) nodupkey;
	by subjectnumberstr llt_name;
	where flag=1;
run;


data final;
	merge aeall overlap_distinct (in=a);
	by subjectnumberstr llt_name;
	if a;
run;
proc sort data=final nodupkey;
	by subjectnumberstr LLT_NAME AESPID AESTDAT AEENDAT;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 11 Jan 2018 19:52:22 GMT</pubDate>
    <dc:creator>djbateman</dc:creator>
    <dc:date>2018-01-11T19:52:22Z</dc:date>
    <item>
      <title>Eliminate Non-Overlapping Terms</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Eliminate-Non-Overlapping-Terms/m-p/426980#M105258</link>
      <description>&lt;P&gt;I have a list of adverse events (AEs), and I am supposed to find if a subject has any overlapping terms and output them into a report.&amp;nbsp; I have been able to do so, but on rare occasions, I have a term that is not overlapping in some places but is in others and is outputting all records.&amp;nbsp; I would appreciate some help finding a way to eliminate the records that are not truly overlapping.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example,&amp;nbsp;Subject 101-002 is showing that "Urinary tract infection" is overlapping, which is true for AESPID 2 &amp;amp; 4; however, AESPID=1 is not overlapping.&amp;nbsp; I want to get rid of this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;SUBJECTNUMBERSTR&lt;/TD&gt;&lt;TD&gt;AESPID&lt;/TD&gt;&lt;TD&gt;LLT_NAME&lt;/TD&gt;&lt;TD&gt;AESTDAT&lt;/TD&gt;&lt;TD&gt;AEENDAT&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;101-002&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;Urinary tract infection&lt;/TD&gt;&lt;TD&gt;03AUG2017&lt;/TD&gt;&lt;TD&gt;15AUG2017&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;SPAN&gt;101-00&lt;/SPAN&gt;&lt;SPAN&gt;2&lt;/SPAN&gt;&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;Urinary tract infection&lt;/TD&gt;&lt;TD&gt;07SEP2017&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;SPAN&gt;101-00&lt;/SPAN&gt;&lt;SPAN&gt;2&lt;/SPAN&gt;&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;Urinary tract infection&lt;/TD&gt;&lt;TD&gt;03OCT2017&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;My method for getting to this point is:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Get a list of all Subject/LLT records where there is a repeat of&amp;nbsp;LLT_NAME&lt;/LI&gt;&lt;LI&gt;Compare all AESTDAT &amp;amp; AEENDAT values to see if there is an overlap and flag&lt;/LI&gt;&lt;LI&gt;Keep distinct Subject/LLT records with a flag&lt;/LI&gt;&lt;LI&gt;Merge the overlapping records with the original data and only keep overlapping terms&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&amp;nbsp;Here is the code I have.&amp;nbsp; Please feel free to modify to help me remove AESPID=1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data AEALL;
	input @1 SUBJECTNUMBERSTR $7. @9 AESPID @13 LLT_NAME $30. @45 AESTDAT date9. @57 AEENDAT date9.;
	format AESTDAT AEENDAT date9.;
	cards;
101-001	1	Acute respiratory failure		05AUG2017	.
101-001	2	Pleural effusion				05AUG2017	.
101-001	3	Pneumonia						07AUG2017	14AUG2017
101-001	4	Septic shock					16AUG2017	22AUG2017
101-001	5	Pneumothorax traumatic			19AUG2017	06SEP2017
101-001	6	Anemia							20AUG2017	.
101-001	7	Pneumothorax traumatic			28AUG2017	09SEP2017
101-001	8	Pneumonia						29AUG2017	08SEP2017
101-001	9	Hypotension						04SEP2017	07SEP2017
101-001	10	Hypernatremia					12AUG2017	23AUG2017
101-001	11	Thrombocytosis					15AUG2017	20AUG2017
101-001	12	Paroxysmal atrial fibrillation	13SEP2017	.
101-001	13	Respiratory infection			15SEP2017	.
101-002	1	Urinary tract infection			03AUG2017	15AUG2017
101-002	2	Urinary tract infection			07SEP2017	.
101-002	3	Pressure ulcer					11SEP2017	.
101-002	4	Urinary tract infection			03OCT2017	.
;
run;


proc sort data=aeall;
	by subjectnumberstr llt_name aestdat aeendat;
	where llt_name^='';
run;

data duplicate;
	set aeall;
	by subjectnumberstr llt_name;
	if first.llt_name &amp;amp; last.llt_name then delete;
run;

proc sort data=duplicate;
	by subjectnumberstr llt_name aespid;
run;


data overlap;
	set duplicate;
	by subjectnumberstr llt_name aespid;
	format prev_stdat prev_endat date9.;
	prev_stdat=lag(aestdat);
	prev_endat=lag(aeendat);
	if first.llt_name then do;
		prev_stdat=.;
		prev_endat=.;
	end;
	else do;
		if (prev_endat^=. &amp;amp; aestdat^=. &amp;amp; prev_stdat&amp;lt;=aestdat &amp;amp; aestdat&amp;lt;=prev_endat) or
			(prev_endat=. &amp;amp; aestdat^=. &amp;amp; prev_stdat&amp;lt;=aestdat) or
			(prev_endat^=. &amp;amp; aestdat^=. &amp;amp; aestdat=prev_endat+1) or
			(aestdat=.) then flag=1;
	end;
run;

proc sort data=overlap out=overlap_distinct (keep=subjectnumberstr llt_name) nodupkey;
	by subjectnumberstr llt_name;
	where flag=1;
run;


data final;
	merge aeall overlap_distinct (in=a);
	by subjectnumberstr llt_name;
	if a;
run;
proc sort data=final nodupkey;
	by subjectnumberstr LLT_NAME AESPID AESTDAT AEENDAT;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jan 2018 19:52:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Eliminate-Non-Overlapping-Terms/m-p/426980#M105258</guid>
      <dc:creator>djbateman</dc:creator>
      <dc:date>2018-01-11T19:52:22Z</dc:date>
    </item>
    <item>
      <title>Re: Eliminate Non-Overlapping Terms</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Eliminate-Non-Overlapping-Terms/m-p/427028#M105265</link>
      <description>&lt;P&gt;I hope I'm not simplifying this too much, to determine when there is an overlap.&amp;nbsp; You might need to fiddle with the conditions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The idea is not to delete anything until the end.&amp;nbsp; Instead, assign a new variable (GROUP) that will be the same for all instances that overlap with one another.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=AEALL;&lt;/P&gt;
&lt;P&gt;by SubjectNumberStr llt_name aestdat aeendat;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data with_groups;&lt;/P&gt;
&lt;P&gt;set AEALL;&lt;/P&gt;
&lt;P&gt;by SubjectNumberStr llt_name aestdat;&lt;/P&gt;
&lt;P&gt;prev_enddate = lag(aeendat);&lt;/P&gt;
&lt;P&gt;if first.llt_name&lt;/P&gt;
&lt;P&gt;or (first.llt_name=0 and&amp;nbsp; (prev_enddate=. or aestdat &amp;lt;= prev_enddate))&lt;/P&gt;
&lt;P&gt;then group + 1;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If GROUP can be accurately defined, the records are easy to subset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data overlapping;&lt;/P&gt;
&lt;P&gt;set with_groups;&lt;/P&gt;
&lt;P&gt;by group;&lt;/P&gt;
&lt;P&gt;if first.group and last.group then delete;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that you don't have to sort by GROUP ... the data set is already sorted.&amp;nbsp; But the trick is to increment GROUP properly.&amp;nbsp; See how close this comes to the result&amp;nbsp; you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jan 2018 21:34:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Eliminate-Non-Overlapping-Terms/m-p/427028#M105265</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-01-11T21:34:13Z</dc:date>
    </item>
    <item>
      <title>Re: Eliminate Non-Overlapping Terms</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Eliminate-Non-Overlapping-Terms/m-p/427040#M105267</link>
      <description>From my initial glance, this seems to be doing what I want. I'll have to dig into this more tomorrow. Thank you for your reply!</description>
      <pubDate>Thu, 11 Jan 2018 21:54:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Eliminate-Non-Overlapping-Terms/m-p/427040#M105267</guid>
      <dc:creator>djbateman</dc:creator>
      <dc:date>2018-01-11T21:54:06Z</dc:date>
    </item>
  </channel>
</rss>

