<?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 Exploding a table based on differences between two tables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Exploding-a-table-based-on-differences-between-two-tables/m-p/726550#M225766</link>
    <description>&lt;P&gt;&lt;SPAN&gt;I am running a program that creates a table &lt;STRONG&gt;new_table&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;everyday. The very next day I want to create the &lt;STRONG&gt;deltas&lt;/STRONG&gt;, meaning I want to create the insert, delete and update data sets&amp;nbsp;between the &lt;STRONG&gt;old&lt;/STRONG&gt; and &lt;STRONG&gt;new&lt;/STRONG&gt; table.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is a minimal reproducible example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data old_table;
infile datalines4 delimiter=",";
input ID $ START END;
format START END yymmddd10.-l;
datalines4;
ID1,20332,2936547
ID2,20361,22249
ID3,20240,22249
ID4,20240,22249
;;;;

data new_table;
infile datalines4 delimiter=",";
input ID $ START END;
format START END yymmddd10.-l;
datalines4;
ID1,20332,22249
ID2,20361,22249
ID4,20240,22249
ID5,20240,20820
;;;;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Basically I want to end up with the following as an output:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Delete table&lt;/STRONG&gt;&lt;/U&gt;:&lt;/P&gt;&lt;P&gt;ID3,20240,22249&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Update table&lt;/STRONG&gt;&lt;/U&gt;:&lt;/P&gt;&lt;P&gt;ID5,20240,20820&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Insert table&lt;/STRONG&gt;&lt;/U&gt;:&lt;/P&gt;&lt;P&gt;ID1,20332,22249&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In summary:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Deletion of ID3&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;(because not in the new table).&lt;/LI&gt;&lt;LI&gt;Update of ID1&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;(because END date is different in the new table).&lt;/LI&gt;&lt;LI&gt;Insert of ID5&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;(because not in old table).&lt;/LI&gt;&lt;LI&gt;Nothing change for ID2&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;and ID4&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;(they are both in new and oldtables and their values does not change).&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;I would like to know what could be the most efficient way of doing this kind of comparison/updates. I was thinking maybe a hash table that would "explode" the table into the three desired output tables might be the go-to option here.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In a second time, I would like to know if it was possible to directly update this table (meaning deleting/inserting and updating the observations at the same time, without creating the intermediary tables like before).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have seen&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/83078"&gt;@SuryaKiran&lt;/a&gt;'s answer on this&amp;nbsp;&lt;A title="Hash Output to update the Initial Dataset" href="https://communities.sas.com/t5/SAS-Procedures/Hash-Output-to-update-the-Initial-Dataset/td-p/479551" target="_self"&gt;Hash Output to update the Initial Dataset&lt;/A&gt;&amp;nbsp;but this seems to be missing the delete and insert actions (it only updated UD1).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;data old_table;  
  if _n_=1 then do;
    declare hash ud(dataset:'new_table');
    ud.defineKey('ID');
    ud.defineData(all:'y');
    ud.defineDone();
  end;
  modify old_table;
  rcUpdate = ud.find();
  if rcUpdate=0 then replace;
run;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Nota Bene: I know that the easy solution is just to replace the old table with the new one. However, in the actual data sets 99% of the observations does not change at all. Hence I only want to "update" the table and not replacing it.&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>Mon, 15 Mar 2021 22:05:35 GMT</pubDate>
    <dc:creator>KermitTheFrog</dc:creator>
    <dc:date>2021-03-15T22:05:35Z</dc:date>
    <item>
      <title>Exploding a table based on differences between two tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Exploding-a-table-based-on-differences-between-two-tables/m-p/726550#M225766</link>
      <description>&lt;P&gt;&lt;SPAN&gt;I am running a program that creates a table &lt;STRONG&gt;new_table&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;everyday. The very next day I want to create the &lt;STRONG&gt;deltas&lt;/STRONG&gt;, meaning I want to create the insert, delete and update data sets&amp;nbsp;between the &lt;STRONG&gt;old&lt;/STRONG&gt; and &lt;STRONG&gt;new&lt;/STRONG&gt; table.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is a minimal reproducible example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data old_table;
infile datalines4 delimiter=",";
input ID $ START END;
format START END yymmddd10.-l;
datalines4;
ID1,20332,2936547
ID2,20361,22249
ID3,20240,22249
ID4,20240,22249
;;;;

data new_table;
infile datalines4 delimiter=",";
input ID $ START END;
format START END yymmddd10.-l;
datalines4;
ID1,20332,22249
ID2,20361,22249
ID4,20240,22249
ID5,20240,20820
;;;;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Basically I want to end up with the following as an output:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Delete table&lt;/STRONG&gt;&lt;/U&gt;:&lt;/P&gt;&lt;P&gt;ID3,20240,22249&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Update table&lt;/STRONG&gt;&lt;/U&gt;:&lt;/P&gt;&lt;P&gt;ID5,20240,20820&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Insert table&lt;/STRONG&gt;&lt;/U&gt;:&lt;/P&gt;&lt;P&gt;ID1,20332,22249&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In summary:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Deletion of ID3&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;(because not in the new table).&lt;/LI&gt;&lt;LI&gt;Update of ID1&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;(because END date is different in the new table).&lt;/LI&gt;&lt;LI&gt;Insert of ID5&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;(because not in old table).&lt;/LI&gt;&lt;LI&gt;Nothing change for ID2&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;and ID4&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;(they are both in new and oldtables and their values does not change).&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;I would like to know what could be the most efficient way of doing this kind of comparison/updates. I was thinking maybe a hash table that would "explode" the table into the three desired output tables might be the go-to option here.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In a second time, I would like to know if it was possible to directly update this table (meaning deleting/inserting and updating the observations at the same time, without creating the intermediary tables like before).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have seen&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/83078"&gt;@SuryaKiran&lt;/a&gt;'s answer on this&amp;nbsp;&lt;A title="Hash Output to update the Initial Dataset" href="https://communities.sas.com/t5/SAS-Procedures/Hash-Output-to-update-the-Initial-Dataset/td-p/479551" target="_self"&gt;Hash Output to update the Initial Dataset&lt;/A&gt;&amp;nbsp;but this seems to be missing the delete and insert actions (it only updated UD1).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;data old_table;  
  if _n_=1 then do;
    declare hash ud(dataset:'new_table');
    ud.defineKey('ID');
    ud.defineData(all:'y');
    ud.defineDone();
  end;
  modify old_table;
  rcUpdate = ud.find();
  if rcUpdate=0 then replace;
run;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Nota Bene: I know that the easy solution is just to replace the old table with the new one. However, in the actual data sets 99% of the observations does not change at all. Hence I only want to "update" the table and not replacing it.&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>Mon, 15 Mar 2021 22:05:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Exploding-a-table-based-on-differences-between-two-tables/m-p/726550#M225766</guid>
      <dc:creator>KermitTheFrog</dc:creator>
      <dc:date>2021-03-15T22:05:35Z</dc:date>
    </item>
    <item>
      <title>Re: Exploding a table based on differences between two tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Exploding-a-table-based-on-differences-between-two-tables/m-p/726719#M225860</link>
      <description>&lt;P&gt;For this minimal example you may use next code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data deleted added updated;
   merge old_table(in=inold rename=(start=_start end=_end)))
              new_table(in=innew);
      by ID;
           if inold and not innew then output deleted; else
           if innew and not inold then output added; else
           if start ne _start or end ne _end then output updated;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In case you need it for various pair tables with different variables and number of variables&lt;/P&gt;
&lt;P&gt;this same code should be rewritten to get dynamically the variable names (from sashelp.vcolumn or sql dictionary.columns) of a table and generate the program to run;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Mar 2021 12:22:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Exploding-a-table-based-on-differences-between-two-tables/m-p/726719#M225860</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-03-16T12:22:36Z</dc:date>
    </item>
  </channel>
</rss>

