<?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 Removing observations based on other observations in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Removing-observations-based-on-other-observations/m-p/346299#M79830</link>
    <description>&lt;P&gt;Hi there,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For your kind information, I am analysing a dataset having patient's records related to his duration of stay in hospital. Some patient may have multiple records of admission during the same visit created by different departments of the same hospital. I am trying to remove all records which are reflecting part of the total duration of stay. For e.g id 101 having one record showing duration of stay from&amp;nbsp;20160110 to 20160120 whereas other records created by a department shows duration of stay from&amp;nbsp;20160115 20160118 which is actually part of his total stay. I want to delete the second record. Can some body help me to address this.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id adm_date  dis_date  site ;
cards;
101 20160110 20160120 1234
101 20160115 20160118 1235
101 20160125 20160128 1235
102 20160110 20160118 1234
102 20160110 20160112 1235
;
run;


data want;
input id adm_date  dis_date  site ;
cards;
101 20160110 20160120 1234
101 20160125 20160128 1235
102 20160110 20160118 1234
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thank you in advance for your kind reply.&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 31 Mar 2017 19:23:41 GMT</pubDate>
    <dc:creator>DeepakSwain</dc:creator>
    <dc:date>2017-03-31T19:23:41Z</dc:date>
    <item>
      <title>Removing observations based on other observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Removing-observations-based-on-other-observations/m-p/346299#M79830</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For your kind information, I am analysing a dataset having patient's records related to his duration of stay in hospital. Some patient may have multiple records of admission during the same visit created by different departments of the same hospital. I am trying to remove all records which are reflecting part of the total duration of stay. For e.g id 101 having one record showing duration of stay from&amp;nbsp;20160110 to 20160120 whereas other records created by a department shows duration of stay from&amp;nbsp;20160115 20160118 which is actually part of his total stay. I want to delete the second record. Can some body help me to address this.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id adm_date  dis_date  site ;
cards;
101 20160110 20160120 1234
101 20160115 20160118 1235
101 20160125 20160128 1235
102 20160110 20160118 1234
102 20160110 20160112 1235
;
run;


data want;
input id adm_date  dis_date  site ;
cards;
101 20160110 20160120 1234
101 20160125 20160128 1235
102 20160110 20160118 1234
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thank you in advance for your kind reply.&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 31 Mar 2017 19:23:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Removing-observations-based-on-other-observations/m-p/346299#M79830</guid>
      <dc:creator>DeepakSwain</dc:creator>
      <dc:date>2017-03-31T19:23:41Z</dc:date>
    </item>
    <item>
      <title>Re: Removing observations based on other observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Removing-observations-based-on-other-observations/m-p/346307#M79835</link>
      <description>&lt;P&gt;I'm not following your logic based on the "want" results.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You're keeping two records for 101, which suggests that the site is part of your grouping, but for 102 you only keep one record, even though they have different sites.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think you're wanting to keep the records that have the largest stay, but I'm wondering if you can clarify.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Or just wait and see if others get it and give you what you want.&lt;/P&gt;</description>
      <pubDate>Fri, 31 Mar 2017 19:40:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Removing-observations-based-on-other-observations/m-p/346307#M79835</guid>
      <dc:creator>collinelliot</dc:creator>
      <dc:date>2017-03-31T19:40:31Z</dc:date>
    </item>
    <item>
      <title>Re: Removing observations based on other observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Removing-observations-based-on-other-observations/m-p/346312#M79838</link>
      <description>&lt;P&gt;What if you have data that looks like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;101 20160110 20160120 1234&lt;BR /&gt;101 20160115 2016&lt;STRONG&gt;0127&lt;/STRONG&gt; 1235&lt;BR /&gt;101 20160125 20160128 1235&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;where you have a partially overlap and not completely within the duration of another visit? Would you still want the second record removed?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 31 Mar 2017 19:47:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Removing-observations-based-on-other-observations/m-p/346312#M79838</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-03-31T19:47:19Z</dc:date>
    </item>
    <item>
      <title>Re: Removing observations based on other observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Removing-observations-based-on-other-observations/m-p/346313#M79839</link>
      <description>Hi collinelliot,&lt;BR /&gt;Here site is not a criteria, date range is the criteria. According to business rule, the largest admission period will be kept and sub-admission record will be deleted.&lt;BR /&gt;Regards,</description>
      <pubDate>Fri, 31 Mar 2017 19:50:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Removing-observations-based-on-other-observations/m-p/346313#M79839</guid>
      <dc:creator>DeepakSwain</dc:creator>
      <dc:date>2017-03-31T19:50:47Z</dc:date>
    </item>
    <item>
      <title>Re: Removing observations based on other observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Removing-observations-based-on-other-observations/m-p/346317#M79841</link>
      <description>&lt;P&gt;Hi &amp;nbsp;ballardw,&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold"&gt;You read my mind. Actually I were waiting to raise this issue in my subsquent mail once I got partial solution to remove the observations having admission period within the maximum duration of admission. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold"&gt;In the second step, I will try to get the extended duration of stay after removing the overlapping period based on the scenario suggested by you, if possible. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold"&gt;In all cases, site number is having no importance.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold"&gt;Regards,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 31 Mar 2017 19:57:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Removing-observations-based-on-other-observations/m-p/346317#M79841</guid>
      <dc:creator>DeepakSwain</dc:creator>
      <dc:date>2017-03-31T19:57:43Z</dc:date>
    </item>
    <item>
      <title>Re: Removing observations based on other observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Removing-observations-based-on-other-observations/m-p/346318#M79842</link>
      <description>&lt;P&gt;Which&lt;EM&gt; site&lt;/EM&gt; do you want to keep?&lt;/P&gt;</description>
      <pubDate>Fri, 31 Mar 2017 19:58:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Removing-observations-based-on-other-observations/m-p/346318#M79842</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2017-03-31T19:58:02Z</dc:date>
    </item>
    <item>
      <title>Re: Removing observations based on other observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Removing-observations-based-on-other-observations/m-p/346322#M79845</link>
      <description>Hi PGStats,&lt;BR /&gt;Site having longer duration of stay will be my priority at first. Later if possible in case there is overlapping duration of stay. i will try to capture the longest duration of stay first, then after removing the overlap period , the extended duration of stay in other site.&lt;BR /&gt;Regards,&lt;BR /&gt;</description>
      <pubDate>Fri, 31 Mar 2017 20:01:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Removing-observations-based-on-other-observations/m-p/346322#M79845</guid>
      <dc:creator>DeepakSwain</dc:creator>
      <dc:date>2017-03-31T20:01:18Z</dc:date>
    </item>
    <item>
      <title>Re: Removing observations based on other observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Removing-observations-based-on-other-observations/m-p/346337#M79854</link>
      <description>&lt;P&gt;For the base requirement I would start with this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
   informat id $5. adm_date  dis_date yymmdd8.  site $5.;
   format adm_date  dis_date yymmdd10.;
   input id adm_date  dis_date  site ;
cards;
101 20160110 20160120 1234
101 20160115 20160118 1235
101 20160125 20160128 1235
102 20160110 20160118 1234
102 20160110 20160112 1235
;
run;
proc sort data=have;
   by id adm_date dis_date;
run;

data reduced;
  set have;
  by id ;
  Ladm= lag(adm_date);
  Ldis= lag(dis_date);
  if not first.id and (ladm le adm_date le ldis) and (ladm le dis_date le ldis) then delete;
  drop ladm ldis;
run;&lt;/PRE&gt;
&lt;P&gt;Note that I am using actual date values. I have a suspicion that somewhere down the line the inteval measures may be needed and determining the duration of the interval 20160228 to 20160305 isn't easy unless you have SAS date values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 31 Mar 2017 20:59:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Removing-observations-based-on-other-observations/m-p/346337#M79854</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-03-31T20:59:12Z</dc:date>
    </item>
    <item>
      <title>Re: Removing observations based on other observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Removing-observations-based-on-other-observations/m-p/346367#M79868</link>
      <description>&lt;P&gt;Here is something you could do. I added id=103, a more complex case involving a succession of stays.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   informat id $5. adm_date  dis_date yymmdd8.  site $5.;
   format adm_date dis_date yymmdd10.;
   input id adm_date dis_date site;
cards;
101 20160110 20160120 1234
101 20160115 20160118 1235
101 20160125 20160128 1235
102 20160110 20160118 1234
102 20160110 20160112 1235
103 20160110 20160112 1234
103 20160112 20160115 1235
103 20160115 20160120 1237
103 20160116 20160118 1236
;

data temp;
set have;
obs = _n_;
run;

proc sql;
create table overlap as
select a.obs as from, b.obs as to
from 
    temp as a inner join 
    temp as b on a.id=b.id and 
        b.adm_date between a.adm_date and a.dis_date; 
*select * from overlap;
quit;

%include "&amp;amp;sasforum\subgraphsmacro.sas" / source2;

%subgraphs(overlap);

proc sql;
create table stays as
select 
    a.clust as stayId,
    b.*
from clusters as a inner join 
    temp as b on a.node=b.obs
order by a.clust, b.adm_date, b.obs;
*select * from stays;
quit;

data want;
do until(last.stayId);
    set stays; by stayId;
    if first.stayId then do;
        adm_d = adm_date;
        adm_site = site;
        end;
    if dis_date &amp;gt;= dis_d then do;
        dis_d = dis_date;
        dis_site = site;
        end;
    end;
output;
format adm_d dis_d yymmdd10.;
keep id adm_d dis_d adm_site dis_site;
run;

proc print data=want noobs; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The subgraphs macro is attached.&lt;/P&gt;</description>
      <pubDate>Sat, 01 Apr 2017 04:04:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Removing-observations-based-on-other-observations/m-p/346367#M79868</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2017-04-01T04:04:17Z</dc:date>
    </item>
    <item>
      <title>Re: Removing observations based on other observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Removing-observations-based-on-other-observations/m-p/346398#M79882</link>
      <description>&lt;P&gt;Find them and Delete them.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id adm_date  dis_date  site ;
cards;
101 20160110 20160120 1234
101 20160115 20160118 1235
101 20160125 20160128 1235
102 20160110 20160118 1234
102 20160110 20160112 1235
;
run;
data have;
 set have;
 by id;
 if first.id then n=0;
 n+1;
run;
proc sql;
create table key as
 select b.*
  from have as a,have as b 
   where a.id=b.id and a.adm_date le b.adm_date and
         a.dis_date gt b.dis_date and a.n ne b.n;
quit;
data want;
 if _n_=1 then do;
  if 0 then set key;
  declare hash h(dataset:'key');
  h.definekey(all:'y');
  h.definedone();
 end;
set have;
if h.check()=0 then delete;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 01 Apr 2017 14:06:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Removing-observations-based-on-other-observations/m-p/346398#M79882</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-04-01T14:06:21Z</dc:date>
    </item>
  </channel>
</rss>

