<?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: Choosing data based on dates and times in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Choosing-data-based-on-dates-and-times/m-p/509541#M15791</link>
    <description>&lt;P&gt;You have a where clause, which my guess is filtering the records. How are your &lt;STRONG&gt;startdatetime&lt;/STRONG&gt; values,&amp;nbsp;&lt;SPAN&gt;&lt;STRONG&gt;4*60*60&lt;/STRONG&gt; might not be the right value.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please provide some sample data and your expected output.&lt;/P&gt;</description>
    <pubDate>Thu, 01 Nov 2018 14:44:57 GMT</pubDate>
    <dc:creator>SuryaKiran</dc:creator>
    <dc:date>2018-11-01T14:44:57Z</dc:date>
    <item>
      <title>Choosing data based on dates and times</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Choosing-data-based-on-dates-and-times/m-p/509534#M15789</link>
      <description>&lt;P&gt;Dear SAS experts&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the following programme where I am trying to merge my original data&amp;nbsp; (data set x with information on multiple events for individual persons) with data on medicine (dataset medicin) given four hours before the event (datetime specified in dateset x (variable called&amp;nbsp;&amp;nbsp;&lt;SPAN&gt;startdatetime)).&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table&amp;nbsp;meds4hoursbefore&amp;nbsp;as&lt;BR /&gt;select x.*, medicin.*&lt;BR /&gt;from x&amp;nbsp;&lt;BR /&gt;left join&lt;BR /&gt;medicin&lt;BR /&gt;on x.Patient_RK = medicin.Patient_RK&lt;BR /&gt;and medicin.medicinadmdatetime between (x.startdatetime - (4*60*60)) and x.startdatetime&amp;nbsp;&lt;BR /&gt;left join&lt;BR /&gt;x next&lt;BR /&gt;on x.Patient_RK = next.Patient_RK&lt;BR /&gt;and x.xnr+1 = next.xnr&lt;BR /&gt;and next.startdatetime &amp;lt; medicin.medicinadmdatetime&lt;BR /&gt;where next.Patient_RK is null&lt;BR /&gt;order by Patient_RK, xnr&lt;BR /&gt;;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The patient id has to match in the two datasets and the programme also has to take into account the multiple events for the individual where we each time has to match with the appropriate medicine data.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My problem is that in dataset x I have 1412 individuals with 4140 events however after merging then I still have 1412 indiduals but only 3785 event. Why am I missing events? It can not be because no medicine data fits the event because some of the 3785 events that remain in the output also dont have at medicine match.&amp;nbsp; I have 2,5 million records in my medicine data.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope this makes sense,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Kind regards&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Solvej&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Nov 2018 14:29:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Choosing-data-based-on-dates-and-times/m-p/509534#M15789</guid>
      <dc:creator>Solvej</dc:creator>
      <dc:date>2018-11-01T14:29:17Z</dc:date>
    </item>
    <item>
      <title>Re: Choosing data based on dates and times</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Choosing-data-based-on-dates-and-times/m-p/509541#M15791</link>
      <description>&lt;P&gt;You have a where clause, which my guess is filtering the records. How are your &lt;STRONG&gt;startdatetime&lt;/STRONG&gt; values,&amp;nbsp;&lt;SPAN&gt;&lt;STRONG&gt;4*60*60&lt;/STRONG&gt; might not be the right value.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please provide some sample data and your expected output.&lt;/P&gt;</description>
      <pubDate>Thu, 01 Nov 2018 14:44:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Choosing-data-based-on-dates-and-times/m-p/509541#M15791</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2018-11-01T14:44:57Z</dc:date>
    </item>
    <item>
      <title>Re: Choosing data based on dates and times</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Choosing-data-based-on-dates-and-times/m-p/509552#M15793</link>
      <description>Thank you. When I remove this I dont loose the patients however at lot more data is matched with my events. I wonder what the statement did. Do you know?&lt;BR /&gt;Kind regards&lt;BR /&gt;</description>
      <pubDate>Thu, 01 Nov 2018 14:59:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Choosing-data-based-on-dates-and-times/m-p/509552#M15793</guid>
      <dc:creator>Solvej</dc:creator>
      <dc:date>2018-11-01T14:59:19Z</dc:date>
    </item>
    <item>
      <title>Re: Choosing data based on dates and times</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Choosing-data-based-on-dates-and-times/m-p/509627#M15799</link>
      <description>&lt;P&gt;That is not a left join works when you have multiple values on the join condition.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Check this example and see the final output.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
input id value;
datalines;
1 123
2 345
;
run;
data two;
input id sales;
datalines;
1 10
1 20
1 30
2 10
2 30
2 40
;
run;

proc sql;
select a.id,a.value,b.sales
from one as a
left join two b
	on a.id=b.id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want only one record from the second table to be joined to first table, then you need to tell which record to be joined. There are several ways to remove duplicate records.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is one example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select a.id,a.value,b.sales
from one as a
left join two b
	on a.id=b.id
group by a.id
having sales=max(sales);
quit;&lt;/CODE&gt;&lt;/PRE&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;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Nov 2018 17:45:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Choosing-data-based-on-dates-and-times/m-p/509627#M15799</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2018-11-01T17:45:37Z</dc:date>
    </item>
  </channel>
</rss>

