<?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 with multiple records in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/merge-two-data-sets-with-multiple-records/m-p/418367#M12785</link>
    <description>&lt;P&gt;Thank you Reeza.&lt;/P&gt;&lt;P&gt;My data sets is too large and I would like to merge the two data set if the type of disease for both data sets are the same and the date should be the nearest date.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 05 Dec 2017 04:49:54 GMT</pubDate>
    <dc:creator>elias_2020</dc:creator>
    <dc:date>2017-12-05T04:49:54Z</dc:date>
    <item>
      <title>merge two data sets with multiple records</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/merge-two-data-sets-with-multiple-records/m-p/418330#M12777</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to merge two data sets when the date of the first data set is equal or the closet date to the second date set date.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;example&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data diagonsed;
   input  id   diag_date mmddyy10.  disease $;
   format diag_date mmddyy10.;
   datalines;
1 05/23/10  C
1 05/26/10  C
1 01/01/11  B
;
run;



data disharge;
   input  id   dis_date mmddyy10.  disease $ hospital$;
   format dis_date mmddyy10.;
   datalines;
1 05/21/10  C    W
1 05/24/10  C    N
1 05/25/10  C    G
1 05/27/10  A    R
1 05/29/10  C    R
1 05/28/10  B    W
1 09/15/10  D    S
1 12/29/10  C    F
1 12/31/10  A    F
1 01/02/11  B    S
1 01/05/11  B    N
;
run;

/*i want to know the name of the hospital for those who were diagnosed for specific disease as in only diagnosed data set*/
/* The diagnosed date should be less or equal to the discharge date (minimum value=dis_date- diag_date) for the same type of disease in the first data set
*/
/* The correct answer should be as below: 
id   diag_date  dis_date  disease    hospital 
1    05/23/10   05/24/10     C             N
1    05/26/10   05/29/10     C             R
1    01/01/11   01/02/11     B              S&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp; for the same type of disease in both data sets, difference_Date=min( dis_date - diag_date ) and it should equal zero or grater than zero.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could you please give me any suggestion.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Tue, 05 Dec 2017 00:47:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/merge-two-data-sets-with-multiple-records/m-p/418330#M12777</guid>
      <dc:creator>elias_2020</dc:creator>
      <dc:date>2017-12-05T00:47:58Z</dc:date>
    </item>
    <item>
      <title>Re: merge two data sets with multiple records</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/merge-two-data-sets-with-multiple-records/m-p/418359#M12782</link>
      <description>&lt;P&gt;Search on here. You’ll find plenty of examples.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can join on nearest using SQL easier than you can via a data step.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your data is not large, you can do a cross join and filter on anything with the date &amp;gt; date2. Then use a data step to pick the lowest/best match.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Dec 2017 03:26:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/merge-two-data-sets-with-multiple-records/m-p/418359#M12782</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-12-05T03:26:47Z</dc:date>
    </item>
    <item>
      <title>Re: merge two data sets with multiple records</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/merge-two-data-sets-with-multiple-records/m-p/418367#M12785</link>
      <description>&lt;P&gt;Thank you Reeza.&lt;/P&gt;&lt;P&gt;My data sets is too large and I would like to merge the two data set if the type of disease for both data sets are the same and the date should be the nearest date.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Dec 2017 04:49:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/merge-two-data-sets-with-multiple-records/m-p/418367#M12785</guid>
      <dc:creator>elias_2020</dc:creator>
      <dc:date>2017-12-05T04:49:54Z</dc:date>
    </item>
    <item>
      <title>Re: merge two data sets with multiple records</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/merge-two-data-sets-with-multiple-records/m-p/418368#M12786</link>
      <description>&lt;P&gt;How about like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data diagnosed;
   input  id   diag_date mmddyy10.  disease $;
   format diag_date mmddyy10.;
   datalines;
1 05/23/10  C
1 05/26/10  C
1 01/01/11  B
;

data discharge;
   input  id   dis_date mmddyy10.  disease $ hospital$;
   format dis_date mmddyy10.;
   datalines;
1 05/21/10  C    W
1 05/24/10  C    N
1 05/25/10  C    G
1 05/27/10  A    R
1 05/29/10  C    R
1 05/28/10  B    W
1 09/15/10  D    S
1 12/29/10  C    F
1 12/31/10  A    F
1 01/02/11  B    S
1 01/05/11  B    N
;

proc sort data=diagnosed; by id disease diag_date; run;
proc sort data=discharge; by id disease dis_date; run;

data all;
retain diag_date;
merge 
    diagnosed(in=ina rename=diag_date=date) 
    discharge(in=inb rename=dis_date=date);
by id disease date;
if first.disease then call missing(diag_date);
if ina then diag_date = date;
if inb and not missing(diag_date) then do;
    dis_date=date;
    output;
    call missing(diag_date);
    end;
format diag_date dis_date mmddyy10.;
drop date;
run;

proc print noobs; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Dec 2017 04:59:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/merge-two-data-sets-with-multiple-records/m-p/418368#M12786</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2017-12-05T04:59:48Z</dc:date>
    </item>
    <item>
      <title>Re: merge two data sets with multiple records</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/merge-two-data-sets-with-multiple-records/m-p/418464#M12791</link>
      <description>&lt;P&gt;How about this one ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data diagnosed;
   input  id   diag_date mmddyy10.  disease $;
   format diag_date mmddyy10.;
   datalines;
1 05/23/10  C
1 05/26/10  C
1 01/01/11  B
;

data discharge;
   input  id   dis_date mmddyy10.  disease $ hospital$;
   format dis_date mmddyy10.;
   datalines;
1 05/21/10  C    W
1 05/24/10  C    N
1 05/25/10  C    G
1 05/27/10  A    R
1 05/29/10  C    R
1 05/28/10  B    W
1 09/15/10  D    S
1 12/29/10  C    F
1 12/31/10  A    F
1 01/02/11  B    S
1 01/05/11  B    N
;
proc sql;
select a.*,b.dis_date,b.hospital
 from diagnosed as a ,discharge as b
  where a.id=b.id and  b.dis_date-a.diag_date&amp;gt;0
   group by a.id,a.diag_date,a.disease
    having b.dis_date-a.diag_date=min(b.dis_date-a.diag_date);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Dec 2017 13:02:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/merge-two-data-sets-with-multiple-records/m-p/418464#M12791</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-12-05T13:02:21Z</dc:date>
    </item>
    <item>
      <title>Re: merge two data sets with multiple records</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/merge-two-data-sets-with-multiple-records/m-p/418651#M12801</link>
      <description>&lt;P&gt;I think this is a case where "set ... by ..." is simpler than "merge ... by ...",&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data diagonsed;
  input  id   diag_date mmddyy10.  disease $;
  format diag_date mmddyy10.;
  datalines;
1 05/23/10  C
1 05/26/10  C
1 01/01/11  B
run;
data disharge;
  input  id   dis_date mmddyy10.  disease $ hospital$;
  format dis_date mmddyy10.;
  datalines;
1 05/21/10  C    W
1 05/24/10  C    N
1 05/25/10  C    G
1 05/27/10  A    R
1 05/29/10  C    R
1 05/28/10  B    W
1 09/15/10  D    S
1 12/29/10  C    F
1 12/31/10  A    F
1 01/02/11  B    S
1 01/05/11  B    N
run;

proc sort data=diagonsed; by id disease diag_date;run;
proc sort data=disharge; by id disease dis_date;run;


data want (rename=(date=dis_date));
  set diagonsed (in=indiag rename=(diag_date=date))
      disharge  (in=indis  rename=(dis_date=date));
  by id disease date;

  diag_date=lag(date); 
  format diag_date mmddyy10.;
  if indis=1 and lag(indiag)=1 and first.disease=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The subsetting IF at the bottom does most of the work.&amp;nbsp; It only keeps cases in which a discharge record immediately follows a diagnosis record.&amp;nbsp; Adding the "and first.disease=0" condition ensures that the consecutive records are for the same id/disease.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;While the "diag_date=lag(date)" sometimes will return a discharge date, all such unwanted cases are filtered out in the subsequent subsetting if.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Dec 2017 01:15:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/merge-two-data-sets-with-multiple-records/m-p/418651#M12801</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-12-06T01:15:55Z</dc:date>
    </item>
    <item>
      <title>Re: merge two data sets with multiple records</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/merge-two-data-sets-with-multiple-records/m-p/418658#M12803</link>
      <description>&lt;P&gt;Thanks Ksharp.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It works with me and I tried it for some cases and it gave me the correct answer.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Dec 2017 01:37:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/merge-two-data-sets-with-multiple-records/m-p/418658#M12803</guid>
      <dc:creator>elias_2020</dc:creator>
      <dc:date>2017-12-06T01:37:01Z</dc:date>
    </item>
  </channel>
</rss>

