<?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: Losing observations while merging with proc sql in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Losing-observation-while-merging-with-proc-sql/m-p/754412#M237903</link>
    <description>&lt;P&gt;Hello Ballardw,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your response. The data has not changed. I only switched&amp;nbsp; computer CPU and reinstalled SAS(same version). The code is still giving me the same&amp;nbsp;&lt;/P&gt;
&lt;P&gt;result. I am still losing variables after the merge.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql;
     create table Newdata as
	 select *
         from New6 left join scheduling_data5 
         on New6.provider = scheduling_data5.provider
         where New6.start_date between scheduling_data5.start_date and scheduling_data5.Shift_end_date;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="UcheOkoro_0-1626369899043.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/61284iF2E486F20AB7099A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="UcheOkoro_0-1626369899043.png" alt="UcheOkoro_0-1626369899043.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 15 Jul 2021 17:26:39 GMT</pubDate>
    <dc:creator>UcheOkoro</dc:creator>
    <dc:date>2021-07-15T17:26:39Z</dc:date>
    <item>
      <title>Losing observation while merging with proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Losing-observation-while-merging-with-proc-sql/m-p/754390#M237883</link>
      <description>&lt;P&gt;Please, I need help merging data with proc sql. It had worked previously but currently I noticed that I was losing observations after merging the two data sets with proc sql. I did not amend my codes. I am losing about 4,431 observations out of 21,513. Thank you in advance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please, find attached the codes and the log message after the merge&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
     create table Newdata as
	 select *
         from New6, scheduling_data5 
         where New6.provider = scheduling_data5.provider
          and New6.start_date between scheduling_data5.start_date and scheduling_data5.Shift_end_date;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="UcheOkoro_0-1626367548739.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/61282iD4F9CF3F3EE830EF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="UcheOkoro_0-1626367548739.png" alt="UcheOkoro_0-1626367548739.png" /&gt;&lt;/span&gt;&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Jul 2021 16:47:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Losing-observation-while-merging-with-proc-sql/m-p/754390#M237883</guid>
      <dc:creator>UcheOkoro</dc:creator>
      <dc:date>2021-07-15T16:47:54Z</dc:date>
    </item>
    <item>
      <title>Re: Losing observation while merging with proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Losing-observation-while-merging-with-proc-sql/m-p/754394#M237887</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where New6.provider = scheduling_data5.provider
          and New6.start_date between scheduling_data5.start_date and scheduling_data5.Shift_end_date;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Clearly, this is where the observations are being deleted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To debug this: try commenting out each condition, one at a time, to see if you can identify the records that are being removed (which should not be removed)&lt;/P&gt;</description>
      <pubDate>Thu, 15 Jul 2021 16:58:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Losing-observation-while-merging-with-proc-sql/m-p/754394#M237887</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-07-15T16:58:52Z</dc:date>
    </item>
    <item>
      <title>Re: Losing observation while merging with proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Losing-observation-while-merging-with-proc-sql/m-p/754399#M237891</link>
      <description>&lt;P&gt;Hello Paige,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your prompt response. Please, I am not sure I understand what you mean by "commenting &lt;SPAN&gt;out each condition, one at a time".&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Jul 2021 17:02:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Losing-observation-while-merging-with-proc-sql/m-p/754399#M237891</guid>
      <dc:creator>UcheOkoro</dc:creator>
      <dc:date>2021-07-15T17:02:44Z</dc:date>
    </item>
    <item>
      <title>Re: Losing observation while merging with proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Losing-observation-while-merging-with-proc-sql/m-p/754402#M237894</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where /* New6.provider = scheduling_data5.provider
          and */ New6.start_date between scheduling_data5.start_date and scheduling_data5.Shift_end_date;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;See what observations return to the results with the above code. Compare to the observations you got when you didn't comment out that part. Are the right observations removed?&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Continue the process with the next parts of the WHERE clause.&lt;/P&gt;</description>
      <pubDate>Thu, 15 Jul 2021 17:14:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Losing-observation-while-merging-with-proc-sql/m-p/754402#M237894</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-07-15T17:14:07Z</dc:date>
    </item>
    <item>
      <title>Re: Losing observation while merging with proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Losing-observation-while-merging-with-proc-sql/m-p/754405#M237897</link>
      <description>&lt;P&gt;You might get better performance with&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;proc sql;
     create table Newdata as
	 select *
         from New6 left join scheduling_data5 
         on New6.provider = scheduling_data5.provider
         where New6.start_date between scheduling_data5.start_date and scheduling_data5.Shift_end_date;
quit;&lt;/LI-CODE&gt;
&lt;P&gt;And you would have all of the providers from New6 even if they do not appear (yet) in the scheduling_data5 data set.&lt;/P&gt;
&lt;P&gt;Your current code is matching every single record with every single record in the other set and then filtering. So you are wasting a lot of clock cycles. The Join on will reduce the number of comparisons as it starts with the providers then filters on the the dates.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since we have the word "provider" I suspect this is medical data and you may find that some "provider" has changed spelling, business identification code or similar in one (if not both) of the data sets if the dates aren't the issue. When code doesn't change and results do the most likely culprit is changes in the data. Be aware that as simple a change as inclusion/removal of a single leading space in value will render what you think should be equal not.&lt;/P&gt;</description>
      <pubDate>Thu, 15 Jul 2021 17:16:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Losing-observation-while-merging-with-proc-sql/m-p/754405#M237897</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-07-15T17:16:40Z</dc:date>
    </item>
    <item>
      <title>Re: Losing observations while merging with proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Losing-observation-while-merging-with-proc-sql/m-p/754412#M237903</link>
      <description>&lt;P&gt;Hello Ballardw,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your response. The data has not changed. I only switched&amp;nbsp; computer CPU and reinstalled SAS(same version). The code is still giving me the same&amp;nbsp;&lt;/P&gt;
&lt;P&gt;result. I am still losing variables after the merge.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql;
     create table Newdata as
	 select *
         from New6 left join scheduling_data5 
         on New6.provider = scheduling_data5.provider
         where New6.start_date between scheduling_data5.start_date and scheduling_data5.Shift_end_date;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="UcheOkoro_0-1626369899043.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/61284iF2E486F20AB7099A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="UcheOkoro_0-1626369899043.png" alt="UcheOkoro_0-1626369899043.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Jul 2021 17:26:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Losing-observation-while-merging-with-proc-sql/m-p/754412#M237903</guid>
      <dc:creator>UcheOkoro</dc:creator>
      <dc:date>2021-07-15T17:26:39Z</dc:date>
    </item>
    <item>
      <title>Re: Losing observations while merging with proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Losing-observation-while-merging-with-proc-sql/m-p/754415#M237906</link>
      <description>&lt;P&gt;I mean losing observations not variables.&lt;/P&gt;</description>
      <pubDate>Thu, 15 Jul 2021 17:38:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Losing-observation-while-merging-with-proc-sql/m-p/754415#M237906</guid>
      <dc:creator>UcheOkoro</dc:creator>
      <dc:date>2021-07-15T17:38:04Z</dc:date>
    </item>
    <item>
      <title>Re: Losing observation while merging with proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Losing-observation-while-merging-with-proc-sql/m-p/754417#M237907</link>
      <description>&lt;P&gt;I attached serial numbers to the unmerged data(main data) and after the merged, I noticed it was deleting serial number 1- 4366.&amp;nbsp; So my data started at serial number 4367.&lt;/P&gt;</description>
      <pubDate>Thu, 15 Jul 2021 17:58:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Losing-observation-while-merging-with-proc-sql/m-p/754417#M237907</guid>
      <dc:creator>UcheOkoro</dc:creator>
      <dc:date>2021-07-15T17:58:24Z</dc:date>
    </item>
    <item>
      <title>Re: Losing observation while merging with proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Losing-observation-while-merging-with-proc-sql/m-p/754420#M237909</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/319831"&gt;@UcheOkoro&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I attached serial numbers to the unmerged data(main data) and after the merged, I noticed it was deleting serial number 1- 4366.&amp;nbsp; So my data started at serial number 4367.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/319831"&gt;@UcheOkoro&lt;/a&gt;&amp;nbsp;we don't want to do this work for you, we want you to do the debugging and dig through the data sets and find out why certain observations are deleted.&lt;/P&gt;</description>
      <pubDate>Thu, 15 Jul 2021 18:14:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Losing-observation-while-merging-with-proc-sql/m-p/754420#M237909</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-07-15T18:14:54Z</dc:date>
    </item>
    <item>
      <title>Re: Losing observations while merging with proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Losing-observation-while-merging-with-proc-sql/m-p/754421#M237910</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/319831"&gt;@UcheOkoro&lt;/a&gt;,&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/319831"&gt;@UcheOkoro&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;The code is still giving me the same&amp;nbsp;result. I am still losing [observations] after the merge.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql;
     create table Newdata as
	 select *
         from New6 left join scheduling_data5 
         on New6.provider = scheduling_data5.provider
         where New6.start_date between scheduling_data5.start_date and scheduling_data5.Shift_end_date;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This is because the providers from &lt;FONT face="courier new,courier"&gt;New6&lt;/FONT&gt; that you may keep additionally by replacing the (implicit) inner join with a left join will hardly meet the WHERE condition with &lt;EM&gt;missing&lt;/EM&gt; &lt;FONT face="courier new,courier"&gt;scheduling_data5.&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;start_date&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;Shift_end_date&lt;/FONT&gt;&amp;nbsp;as they are not contained in &lt;FONT face="courier new,courier"&gt;scheduling_data5&lt;/FONT&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a small example demonstrating how observations get "lost" with your initial code -- because the provider ID is not contained in both datasets or the condition involving the dates is not met -- and how you can keep some or all of them with other types of joins:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create sample data for demonstration */

data n6;
input prov startd;
cards;
1 20
2 10
3 30
4 15
;

data schd5;
input startd endd prov;
cards;
22 29 1
17 31 3
10 24 5
;

/* Inner join similar to your initial code */

proc sql;
create table nd as
select *
from n6, schd5 
where n6.prov = schd5.prov
      and n6.startd between schd5.startd and schd5.endd;
quit; /* 1 obs., two warnings in the log */

/* Left join */

proc sql;
create table nd(drop=sprov) as
select *
from n6(rename=(startd=nstartd)) left join schd5(rename=(prov=sprov))
on prov = sprov and nstartd between schd5.startd and schd5.endd;
quit; /* 4 obs., no warnings */

/* Right join */

proc sql;
create table nd(drop=nprov) as
select *
from n6(rename=(startd=nstartd prov=nprov)) right join schd5
on nprov = prov and nstartd between schd5.startd and schd5.endd;
quit; /* 3 obs., no warnings */

/* Full join */

proc sql;
create table nd as
select *
from n6(rename=(startd=nstartd prov=nprov)) full join schd5
on nprov = prov and nstartd between schd5.startd and schd5.endd;
quit; /* 6 obs., no warnings */&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 15 Jul 2021 18:19:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Losing-observation-while-merging-with-proc-sql/m-p/754421#M237910</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-07-15T18:19:32Z</dc:date>
    </item>
    <item>
      <title>Re: Losing observation while merging with proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Losing-observation-while-merging-with-proc-sql/m-p/754442#M237918</link>
      <description>&lt;P&gt;The problem was with the way SAS was importing the datetime variable. I was using SAS(English with DBCS) instead of SAS(English) and it was converting some mmddyyyy observations to ddmmyyyy. It completely altered the datetime variable. Thank you all for your assistance&lt;/P&gt;</description>
      <pubDate>Thu, 15 Jul 2021 20:39:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Losing-observation-while-merging-with-proc-sql/m-p/754442#M237918</guid>
      <dc:creator>UcheOkoro</dc:creator>
      <dc:date>2021-07-15T20:39:09Z</dc:date>
    </item>
  </channel>
</rss>

