<?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: Merging Selected cases within specified time (Proc SQL) in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Merging-Selected-cases-within-specified-time-Proc-SQL/m-p/363685#M64623</link>
    <description>&lt;P&gt;I have done step by step&lt;/P&gt;&lt;P&gt;In case your final result have data1 with no UD_collection_date and UM_Collection_date then remove those record.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Sorting data2 descending order*/&lt;BR /&gt;proc sort data=data2 out= data2_24hrs;
by descending Ud_COLLECTION_DATE;
run;

data data2_latest;
set data2_24hrs;
by descending Ud_COLLECTION_DATE;
if first.Reg_No and Ud_COLLECTION_DATE &amp;gt; datetime() -86400 then output;
run;
&lt;BR /&gt;/* Sorting data 3 desending order*/
proc sort data=data3 out= data3_24hrs;
by descending Um_COLLECTION_DATE
run;

data data3_latest;
set data3_24hrs;
by descending Um_COLLECTION_DATE
if first.Reg_No and Um_COLLECTION_DATE&amp;gt; datetime() -86400 then output;
run;



proc sql;
create table want as
select
a.Reg_no
,a.collection_date
,b.Ud_COLLECTION_DATE 
,c.Um_COLLECTION_DATE
from data1 a left join data2_latest b on a.Reg_no = b.Reg_no
 left join data3_latest c on a.Reg_no = c.Reg_no
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 02 Jun 2017 03:15:56 GMT</pubDate>
    <dc:creator>RahulG</dc:creator>
    <dc:date>2017-06-02T03:15:56Z</dc:date>
    <item>
      <title>Merging Selected cases within specified time (Proc SQL)</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Merging-Selected-cases-within-specified-time-Proc-SQL/m-p/363604#M64618</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;I am confused how to get the correct merged data set for my analysis. Any help with this is greatly appreciated.&lt;/P&gt;&lt;P&gt;Thanks in Advance!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have 3 datasets: &lt;STRONG&gt;Data1, Data2 and Data3&lt;/STRONG&gt;. There are patients in Data1 which overlap with patients in Data2 and Data3.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Patients in Data1 are of interest&lt;/STRONG&gt; and I need to merge contents of Data2 and Data3 for only those patients in Data1.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Reg_No denotes unique patient&lt;/P&gt;&lt;P&gt;COLLECTION_DATE denotes time when Test A is done&lt;/P&gt;&lt;P&gt;Ud_COLLECTION_DATE denotes time when Test B is done&lt;/P&gt;&lt;P&gt;Um_COLLECTION_DATE denotes time when Test C is done&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data1 contains Reg_No, COLLECTION_DATE, Result_Uc.&lt;/P&gt;&lt;P&gt;Data2 Contains Reg_No, Ud_COLLECTION_DATE, Result_Ud.&lt;/P&gt;&lt;P&gt;Data3 Contains Reg_No, Um_COLLECTION_DATE, Result_Um.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;All patients (Reg_Nos) have COLLECTION_DATE, but may or may not have corresponding Ud_COLLECTION_DATE and Um_COLLECTION_DATE.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Each patient (Reg_No) can have multiple Tests (Test A) recorded by different COLLECTION_DATE. This is captured in Data1.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;For this Data1, I need to merge patients from Data2 if they have any Test B (Ud_COLLECTION_DATE) within 24 hours of Test A (COLLECTION_DATE).&lt;/STRONG&gt; &lt;STRONG&gt;If&lt;/STRONG&gt; there are &lt;STRONG&gt;multiple Test Bs&lt;/STRONG&gt; (Ud_COLLECTION_DATEs) within 24 hours of Test A (COLLECTION_DATE) then &lt;STRONG&gt;I need to pick only the closest Test B to Test A&lt;/STRONG&gt; and not the rest of Test Bs for each Reg_No.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Similarly, I need to merge patients from Data3 if they have any Test C (Um_COLLECTION_DATE) within 24 hours of Test A (COLLECTION_DATE).&lt;/STRONG&gt; &lt;STRONG&gt;If&lt;/STRONG&gt; there are &lt;STRONG&gt;multiple Test Cs&lt;/STRONG&gt; (Um_COLLECTION_DATEs) within 24 hours of Test A (COLLECTION_DATE) then &lt;STRONG&gt;I need to pick only the closest Test C to Test A&lt;/STRONG&gt; and not the rest of Test Cs for each Reg_No.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Basically &lt;STRONG&gt;I need to create a variable&lt;/STRONG&gt; if patients got any &lt;STRONG&gt;Test B or Test C within 24 hours of Test A&lt;/STRONG&gt; for all patients in Data1.&lt;/P&gt;&lt;P&gt;Please see the data sets below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For the sake of explanation, I placed some Reg_Nos in Data2 and Data3 are empty without corresponding dates indicating that they have Test A but not Test B and C (but in actual data2 and data3, if they do not have Test B or Test C, they do not have any record of Reg_No).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Let me know if you have any questions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data1;
	informat Reg_No $12. COLLECTION_DATE datetime20.;
	format Reg_No $12. COLLECTION_DATE datetime20.;
	
	input Reg_No   $   COLLECTION_DATE;
	cards;
100024536387	06JUN2016:16:25:00
100024536431	05JUN2016:17:39:00
100024536472	05JUN2016:03:30:00
100024536728	09OCT2016:13:54:00
100024536755	09OCT2016:08:54:00
100024536755	11OCT2016:08:49:00
100024536794	09OCT2016:12:45:00
100024536808	09OCT2016:10:26:00
100024580163	04JUN2016:08:00:00
100024580277	07JUL2016:09:39:00
110004803082	07JUL2016:12:39:00
110004803191	24JUN2016:17:35:00
110004803378	29JUN2016:19:08:00
110004803693	06SEP2016:00:13:00
110004804149	22OCT2016:21:03:00
110004804305	31OCT2016:11:30:00
110004804502	20NOV2016:22:08:00
110004809370	12JUN2016:13:46:00
110004809859	24DEC2016:07:18:00
110004810284	17JAN2017:11:42:00
110004810284	18JAN2017:01:07:00
110004810756	26JAN2017:01:00:00
110004810951	02FEB2017:16:16:00
110004811079	08FEB2017:22:25:00
110004811139	13FEB2017:14:37:00
110004811242	07JUL2016:04:56:00
110004811773	14OCT2016:16:36:00
110004811819	27NOV2016:05:36:00
110004812199	16JAN2017:04:16:00
110004812239	10FEB2017:15:27:00
110004812246	19JAN2017:22:06:00
110004812426	09SEP2016:18:39:00
110004812430	04SEP2016:18:14:00
110004812430	09SEP2016:18:05:00
110004812444	15SEP2016:00:24:00
	;
	run;


Data2;
	informat Reg_No $12. Ud_COLLECTION_DATE datetime20.;
	format Reg_No $12. Ud_COLLECTION_DATE datetime20.;
	
	input Reg_No   $   Ud_COLLECTION_DATE;
	cards;
100024536387	06JUN2016:09:25:00
100024536431	05JUN2016:17:39:00
100024536472	05JUN2016:03:30:00
100024536472	04JUN2016:15:50:00
100024536728	
100024536755	09OCT2016:08:57:00
100024536755	11OCT2016:08:51:00
100024536794	
100024536808	09OCT2016:10:28:00
100024580163	
100024580277	
110004803082	07JUL2016:12:45:00
110004803082	07JUL2016:12:45:00
110004803191	24JUN2016:07:36:00
110004803378	29JUN2016:10:46:00
110004803693	05SEP2016:17:14:00
110004803693	05SEP2016:22:23:00
110004804149	
110004804305	
110004804502	20NOV2016:22:09:00
110004804502	21NOV2016:07:52:00
110004809370	
110004809859	24DEC2016:16:34:00
110004810284	16JAN2017:23:08:00
110004810284	17JAN2017:14:12:00
110004810284	17JAN2017:22:26:00
110004810756	25JAN2017:19:27:00
110004810756	26JAN2017:01:03:00
110004810951	01FEB2017:19:22:00
110004810951	01FEB2017:21:07:00
110004811079	
110004811139	13FEB2017:15:01:00
110004811242	07JUL2016:04:56:00
110004811773	
110004811819	27NOV2016:05:36:00
110004812199	
110004812239	09FEB2017:21:01:00
110004812246	19JAN2017:15:20:00
110004812426	09SEP2016:03:07:00
110004812426	09SEP2016:18:39:00
110004812430	04SEP2016:09:37:00
110004812430	04SEP2016:09:37:00
110004812444	14SEP2016:07:38:00
;
Run;

Data3;
	informat Reg_No $12. Um_COLLECTION_DATE datetime20.;
	format Reg_No $12. Um_COLLECTION_DATE datetime20.;
	
	input Reg_No   $   Um_COLLECTION_DATE;
	cards;
100024536387	06JUN2016:09:25:00
100024536431	05JUN2016:17:39:00
100024536472	05JUN2016:03:30:00
100024536728	
100024536755	09OCT2016:08:57:00
100024536755	11OCT2016:08:51:00
100024536794	
100024536808	09OCT2016:10:28:00
100024580163	04JUN2016:04:10:00
100024580277	
110004803082	07JUL2016:12:45:00
110004803191	24JUN2016:07:36:00
110004803378	29JUN2016:10:46:00
110004803693	05SEP2016:17:14:00
110004803693	05SEP2016:22:23:00
110004803693	05SEP2016:23:30:00
110004804149	
110004804305	
110004804502	20NOV2016:22:09:00
110004804502	21NOV2016:07:52:00
110004809370	
110004809859	
110004810284	16JAN2017:23:08:00
110004810284	17JAN2017:22:26:00
110004810756	25JAN2017:19:27:00
110004810756	26JAN2017:01:03:00
110004810951	01FEB2017:19:22:00
110004810951	01FEB2017:21:07:00
110004811079	
110004811139	13FEB2017:15:01:00
110004811242	07JUL2016:04:56:00
110004811773	14OCT2016:10:48:00
110004811819	27NOV2016:05:36:00
110004812199	16JAN2017:00:39:00
110004812199	16JAN2017:03:46:00
110004812239	
110004812246	19JAN2017:15:20:00
110004812426	09SEP2016:18:39:00
110004812430	04SEP2016:09:37:00
110004812430	09SEP2016:14:29:00
110004812430	09SEP2016:16:24:00
110004812444	14SEP2016:07:38:00
;
Run;






&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 01 Jun 2017 21:00:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Merging-Selected-cases-within-specified-time-Proc-SQL/m-p/363604#M64618</guid>
      <dc:creator>sms1891</dc:creator>
      <dc:date>2017-06-01T21:00:23Z</dc:date>
    </item>
    <item>
      <title>Re: Merging Selected cases within specified time (Proc SQL)</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Merging-Selected-cases-within-specified-time-Proc-SQL/m-p/363685#M64623</link>
      <description>&lt;P&gt;I have done step by step&lt;/P&gt;&lt;P&gt;In case your final result have data1 with no UD_collection_date and UM_Collection_date then remove those record.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Sorting data2 descending order*/&lt;BR /&gt;proc sort data=data2 out= data2_24hrs;
by descending Ud_COLLECTION_DATE;
run;

data data2_latest;
set data2_24hrs;
by descending Ud_COLLECTION_DATE;
if first.Reg_No and Ud_COLLECTION_DATE &amp;gt; datetime() -86400 then output;
run;
&lt;BR /&gt;/* Sorting data 3 desending order*/
proc sort data=data3 out= data3_24hrs;
by descending Um_COLLECTION_DATE
run;

data data3_latest;
set data3_24hrs;
by descending Um_COLLECTION_DATE
if first.Reg_No and Um_COLLECTION_DATE&amp;gt; datetime() -86400 then output;
run;



proc sql;
create table want as
select
a.Reg_no
,a.collection_date
,b.Ud_COLLECTION_DATE 
,c.Um_COLLECTION_DATE
from data1 a left join data2_latest b on a.Reg_no = b.Reg_no
 left join data3_latest c on a.Reg_no = c.Reg_no
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Jun 2017 03:15:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Merging-Selected-cases-within-specified-time-Proc-SQL/m-p/363685#M64623</guid>
      <dc:creator>RahulG</dc:creator>
      <dc:date>2017-06-02T03:15:56Z</dc:date>
    </item>
    <item>
      <title>Re: Merging Selected cases within specified time (Proc SQL)</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Merging-Selected-cases-within-specified-time-Proc-SQL/m-p/363851#M64637</link>
      <description>&lt;P&gt;Rahul,&lt;/P&gt;&lt;P&gt;Thanks for the reply. Does your code takes into account only those Ud_Coolection_Date and Um_Collection_Date within 24 hours of Collection_Date from Data1?&lt;/P&gt;&lt;P&gt;That is what I am looking for.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Fri, 02 Jun 2017 15:10:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Merging-Selected-cases-within-specified-time-Proc-SQL/m-p/363851#M64637</guid>
      <dc:creator>sms1891</dc:creator>
      <dc:date>2017-06-02T15:10:25Z</dc:date>
    </item>
    <item>
      <title>Re: Merging Selected cases within specified time (Proc SQL)</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Merging-Selected-cases-within-specified-time-Proc-SQL/m-p/364194#M64668</link>
      <description>&lt;P&gt;This piece of code would take latest collection date within 24 hrs. In case there are two collection within 24 hours, only latest one would be considered &amp;nbsp; "first.regno"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; data3_latest&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token keyword"&gt;set&lt;/SPAN&gt; data3_24hrs&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token statement"&gt;by&lt;/SPAN&gt; descending Um_COLLECTION_DATE
&lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;first&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;Reg_No and Um_COLLECTION_DATE&lt;SPAN class="token operator"&gt;&amp;gt;&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;datetime&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;-86400&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; output&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;datetime() - 86400 would find the collection date within 24 hours.&lt;/P&gt;</description>
      <pubDate>Mon, 05 Jun 2017 03:17:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Merging-Selected-cases-within-specified-time-Proc-SQL/m-p/364194#M64668</guid>
      <dc:creator>RahulG</dc:creator>
      <dc:date>2017-06-05T03:17:45Z</dc:date>
    </item>
    <item>
      <title>Re: Merging Selected cases within specified time (Proc SQL)</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Merging-Selected-cases-within-specified-time-Proc-SQL/m-p/364564#M64682</link>
      <description>&lt;P&gt;How does this&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;first&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;Reg_No and Ud_COLLECTION_DATE &lt;SPAN class="token operator"&gt;&amp;gt;&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;datetime&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;-86400&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; output&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;calculate Ud_COLLECTION_DATE within 24 hours of COLLECTION_DATE which is actually a variable in Data1 and not Data2?&lt;/P&gt;</description>
      <pubDate>Tue, 06 Jun 2017 13:12:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Merging-Selected-cases-within-specified-time-Proc-SQL/m-p/364564#M64682</guid>
      <dc:creator>sms1891</dc:creator>
      <dc:date>2017-06-06T13:12:46Z</dc:date>
    </item>
    <item>
      <title>Re: Merging Selected cases within specified time (Proc SQL)</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Merging-Selected-cases-within-specified-time-Proc-SQL/m-p/364823#M64691</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table data1_data2 as
select
a.Reg_no
,a.collection_date
,b.Ud_COLLECTION_DATE 
from data1 a left join data2_latest b on a.Reg_no = b.Reg_no
;
quit;

proc sort data =data1_data2 ; 
by Reg_No descending collection_date descending  Ud_COLLECTION_DATE;
run;

data data2_latest;
set =data1_data2 ;
by descending collection_date descending  Ud_COLLECTION_DATE;
if first.Reg_No and Ud_COLLECTION_DATE &amp;gt; collection_date -86400 then output;
run;

proc sql;
create table data1_data3 as
select
a.Reg_no
,a.collection_date
,c.Um_COLLECTION_DATE
from data1 a  left join data3_latest c on a.Reg_no = c.Reg_no
;
quit;

data data3_latest;
set =data1_data3 ;
by descending collection_date descending  Um_COLLECTION_DATE
if first.Reg_No and Um_COLLECTION_DATE&amp;gt; collection_date -86400 then output;
run;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;If you are happy with data2_latest and data3_latest then merge these two dataset&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select
a.Reg_no
,a.collection_date
,b.Ud_COLLECTION_DATE 
,c.Um_COLLECTION_DATE
from data1 a left join data2_latest b on a.Reg_no = b.Reg_no
 left join data3_latest c on a.Reg_no = c.Reg_no
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 07 Jun 2017 05:24:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Merging-Selected-cases-within-specified-time-Proc-SQL/m-p/364823#M64691</guid>
      <dc:creator>RahulG</dc:creator>
      <dc:date>2017-06-07T05:24:18Z</dc:date>
    </item>
  </channel>
</rss>

