<?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: Merge two data sets when the visit hospital between  two events  dates in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Merge-two-data-sets-when-the-visit-hospital-between-two-events/m-p/420018#M12870</link>
    <description>here is a sample code which would do good with the example, but you will need to modify it when there are more than 2 offence_date.&lt;BR /&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table admit as&lt;BR /&gt;select *&lt;BR /&gt;from (&lt;BR /&gt;select distinct a.id,admit_date format mmddyy10.,admit_date as offence_date format mmddyy10.,sex&lt;BR /&gt;from (&lt;BR /&gt;select id,offence_date format mmddyy10., min(offence_date) as min format mmddyy10.,&lt;BR /&gt;case when count(id) =1 then today() else max(offence_date) end as max format mmddyy10.,&lt;BR /&gt;sex&lt;BR /&gt;from offenders&lt;BR /&gt;group by id&lt;BR /&gt;) a&lt;BR /&gt;left join hospital b&lt;BR /&gt;on a.id= b.id&lt;BR /&gt;and admit_date between min and max&lt;BR /&gt;)&lt;BR /&gt;where admit_date is not null&lt;BR /&gt;order by id,offence_date&lt;BR /&gt;;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;proc sort data=offenders; by id offence_date; run;&lt;BR /&gt;&lt;BR /&gt;data fin;&lt;BR /&gt;merge offenders admit;&lt;BR /&gt;by id offence_date;&lt;BR /&gt;if offence_date= admit_date then call missing(offence_date);&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
    <pubDate>Mon, 11 Dec 2017 06:12:34 GMT</pubDate>
    <dc:creator>RM6</dc:creator>
    <dc:date>2017-12-11T06:12:34Z</dc:date>
    <item>
      <title>Merge two data sets when the visit hospital between  two events  dates</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Merge-two-data-sets-when-the-visit-hospital-between-two-events/m-p/419993#M12868</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data offenders;
   input  id   offence_DATE mmddyy10. sex$;
   format offence_DATE  mmddyy10.;
   datalines;
1 05/23/05  M 
1 08/01/09  M
2 04/18/01  F
2 06/10/12  F
3 01/01/11  M
;
run;



data HOSPITAL;
   input  id   ADMIT_date mmddyy10. ;
   format ADMIT_date mmddyy10.;
   datalines;
1 10/21/03      
1 06/29/05      
1 02/03/07 
1 09/21/08
1 08/17/09         
3 12/31/10      
3 01/02/13      
;
run;

/* I would like to know any hospital visit between the two offences (if the offenders has two) or any visit after the first offence (fot those who has only one offence such as ID=3)*/
/* I would like to keep the offender who doesn’t have any visit in the data*/ 
/*The result should be appear as below

ID  offence_DATE   ADMIT_date   sex
1   05/23/05                    M
1                   06/29/05    M
1                   02/03/07    M
1                   09/21/08    M
1  08/01/09                     M
2  04/18/01                     F 
2  06/10/12                     F
3  01/01/11                     M
3                   01/02/13    M               

/*&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Could you please&amp;nbsp;suggest for the best method for merging such this kind of the data.&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;Thank you.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Dec 2017 01:27:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Merge-two-data-sets-when-the-visit-hospital-between-two-events/m-p/419993#M12868</guid>
      <dc:creator>elias_2020</dc:creator>
      <dc:date>2017-12-11T01:27:56Z</dc:date>
    </item>
    <item>
      <title>Re: Merge two data sets when the visit hospital between  two events  dates</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Merge-two-data-sets-when-the-visit-hospital-between-two-events/m-p/420018#M12870</link>
      <description>here is a sample code which would do good with the example, but you will need to modify it when there are more than 2 offence_date.&lt;BR /&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table admit as&lt;BR /&gt;select *&lt;BR /&gt;from (&lt;BR /&gt;select distinct a.id,admit_date format mmddyy10.,admit_date as offence_date format mmddyy10.,sex&lt;BR /&gt;from (&lt;BR /&gt;select id,offence_date format mmddyy10., min(offence_date) as min format mmddyy10.,&lt;BR /&gt;case when count(id) =1 then today() else max(offence_date) end as max format mmddyy10.,&lt;BR /&gt;sex&lt;BR /&gt;from offenders&lt;BR /&gt;group by id&lt;BR /&gt;) a&lt;BR /&gt;left join hospital b&lt;BR /&gt;on a.id= b.id&lt;BR /&gt;and admit_date between min and max&lt;BR /&gt;)&lt;BR /&gt;where admit_date is not null&lt;BR /&gt;order by id,offence_date&lt;BR /&gt;;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;proc sort data=offenders; by id offence_date; run;&lt;BR /&gt;&lt;BR /&gt;data fin;&lt;BR /&gt;merge offenders admit;&lt;BR /&gt;by id offence_date;&lt;BR /&gt;if offence_date= admit_date then call missing(offence_date);&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 11 Dec 2017 06:12:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Merge-two-data-sets-when-the-visit-hospital-between-two-events/m-p/420018#M12870</guid>
      <dc:creator>RM6</dc:creator>
      <dc:date>2017-12-11T06:12:34Z</dc:date>
    </item>
    <item>
      <title>Re: Merge two data sets when the visit hospital between  two events  dates</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Merge-two-data-sets-when-the-visit-hospital-between-two-events/m-p/420081#M12873</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data offenders;
   input  id   offence_DATE mmddyy10. sex$;
   date=offence_DATE ;
   format offence_DATE  date mmddyy10.;
   datalines;
1 05/23/05  M 
1 08/01/09  M
2 04/18/01  F
2 06/10/12  F
3 01/01/11  M
;
run;



data HOSPITAL;
   input  id   ADMIT_date mmddyy10. ;
   date=ADMIT_date;
   format ADMIT_date date mmddyy10.;
   datalines;
1 10/21/03      
1 06/29/05      
1 02/03/07 
1 09/21/08
1 08/17/09         
3 12/31/10      
3 01/02/13      
;
run;

data temp;
 set offenders HOSPITAL;
 by id date;
 drop date;
run;
data want;
 merge temp(drop=sex) temp(keep=id sex where=(sex is not missing));
 by id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 11 Dec 2017 12:26:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Merge-two-data-sets-when-the-visit-hospital-between-two-events/m-p/420081#M12873</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-12-11T12:26:26Z</dc:date>
    </item>
    <item>
      <title>Re: Merge two data sets when the visit hospital between  two events  dates</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Merge-two-data-sets-when-the-visit-hospital-between-two-events/m-p/420308#M12879</link>
      <description>&lt;P&gt;Thank you RM6.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Dec 2017 23:14:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Merge-two-data-sets-when-the-visit-hospital-between-two-events/m-p/420308#M12879</guid>
      <dc:creator>elias_2020</dc:creator>
      <dc:date>2017-12-11T23:14:21Z</dc:date>
    </item>
    <item>
      <title>Re: Merge two data sets when the visit hospital between  two events  dates</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Merge-two-data-sets-when-the-visit-hospital-between-two-events/m-p/420309#M12880</link>
      <description>&lt;P&gt;Thank you so much Ksharp.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It gives me the correct solution as well.&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>Mon, 11 Dec 2017 23:15:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Merge-two-data-sets-when-the-visit-hospital-between-two-events/m-p/420309#M12880</guid>
      <dc:creator>elias_2020</dc:creator>
      <dc:date>2017-12-11T23:15:13Z</dc:date>
    </item>
  </channel>
</rss>

