<?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 index when updating table with hash replace option in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Losing-index-when-updating-table-with-hash-replace-option/m-p/919063#M362014</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76464"&gt;@s_lassen&lt;/a&gt;&amp;nbsp;That can't be the solution. And update in place can't change the table structure and though either IF 0 THEN SET DIM; is not required or more likely this code is not going to do what's intended.&lt;/P&gt;</description>
    <pubDate>Wed, 06 Mar 2024 10:59:23 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2024-03-06T10:59:23Z</dc:date>
    <item>
      <title>Losing index when updating table with hash replace option</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Losing-index-when-updating-table-with-hash-replace-option/m-p/919046#M362002</link>
      <description>&lt;P&gt;Hello All,&lt;/P&gt;
&lt;P&gt;I am using the below logic to update the fact table, but I am losing index... do we have a method where we repace the required rows and also retain index..?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data fact;&lt;BR /&gt;if 0 then set dim fact;&lt;BR /&gt;if _n_=1 then do;&lt;BR /&gt;declare hash h1(dataset:'dim(where=( roll=1))');&lt;BR /&gt;h1.definekey('roll','roll1');&lt;BR /&gt;h1.definedata(all:'yes');&lt;BR /&gt;h1.definedone();&lt;BR /&gt;end;&lt;BR /&gt;set fact;&lt;BR /&gt;if roll&amp;lt;3 then do;&lt;BR /&gt;if h1.find()=0 then h1.replace();&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;Mushy&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Mar 2024 09:59:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Losing-index-when-updating-table-with-hash-replace-option/m-p/919046#M362002</guid>
      <dc:creator>Mushy</dc:creator>
      <dc:date>2024-03-06T09:59:39Z</dc:date>
    </item>
    <item>
      <title>Re: Losing index when updating table with hash replace option</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Losing-index-when-updating-table-with-hash-replace-option/m-p/919049#M362004</link>
      <description>&lt;P&gt;You can use the MODIFY statement instead of the SET statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So it could be done like&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data fact;
  if 0 then
    set dim;
  if _n_=1 then do;
    declare hash h1(dataset:'dim(where=( roll=1))');
    h1.definekey('roll','roll1');
    h1.definedata(all:'yes');
    h1.definedone();
    end;
  modify fact;
  if roll&amp;lt;3 then do;
    if h1.find()=0 then h1.replace();
    replace fact;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I put the REPLACE statement inside IF block as no variables are changed if ROLL&amp;gt;=3, so the observation stays the same.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Mar 2024 10:18:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Losing-index-when-updating-table-with-hash-replace-option/m-p/919049#M362004</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2024-03-06T10:18:29Z</dc:date>
    </item>
    <item>
      <title>Re: Losing index when updating table with hash replace option</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Losing-index-when-updating-table-with-hash-replace-option/m-p/919054#M362009</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/393424"&gt;@Mushy&lt;/a&gt;&amp;nbsp;You can use the data step modify or update statement to update a SAS table in place which&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76464"&gt;@s_lassen&lt;/a&gt;&amp;nbsp;has already demonstrated. BUT... based on your code you want also to add variables as well - the one from the dimension table as I understand it - and then an update in place won't work anymore unless you first add the additional columns with a SQL Alter Table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The by far easiest way is to just create a new table including creation of the indexes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Looking at the code you're sharing and besides that a fact table shouldn't require updates I have some questions what you're actually trying to achieve.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Given your code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data fact;
  if 0 then set dim fact;
  if _n_=1 then
    do;
      declare hash h1(dataset:'dim(where=( roll=1))');
      h1.definekey('roll','roll1');
      h1.definedata(all:'yes');
      h1.definedone();
    end;

  set fact;

  if roll&amp;lt;3 then
    do;
      if h1.find()=0 then h1.replace();
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This loads a hash with rows where ROLL always got a value of 1&lt;/P&gt;
&lt;PRE&gt;declare hash h1(dataset:'dim(where=( roll=1))');&lt;/PRE&gt;
&lt;P&gt;Then what's the purpose of below if ROLL is always 1 in the hash&lt;/P&gt;
&lt;PRE&gt;if roll&amp;lt;3 then&lt;/PRE&gt;
&lt;P&gt;And below the replace doesn't make any sense. If the condition is true then the variables in the data set will be overwritten with the values from the hash. The replace() method comes after and at the moment when the values in the data set (the pdv) and the matching item in the hash table have the same values - so replace() makes no sense.&lt;/P&gt;
&lt;PRE&gt;if h1.find()=0 then h1.replace();&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Mar 2024 10:55:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Losing-index-when-updating-table-with-hash-replace-option/m-p/919054#M362009</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-03-06T10:55:30Z</dc:date>
    </item>
    <item>
      <title>Re: Losing index when updating table with hash replace option</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Losing-index-when-updating-table-with-hash-replace-option/m-p/919062#M362013</link>
      <description>&lt;P&gt;thanks for helping me out&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76464"&gt;@s_lassen&lt;/a&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Mar 2024 10:55:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Losing-index-when-updating-table-with-hash-replace-option/m-p/919062#M362013</guid>
      <dc:creator>Mushy</dc:creator>
      <dc:date>2024-03-06T10:55:34Z</dc:date>
    </item>
    <item>
      <title>Re: Losing index when updating table with hash replace option</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Losing-index-when-updating-table-with-hash-replace-option/m-p/919063#M362014</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76464"&gt;@s_lassen&lt;/a&gt;&amp;nbsp;That can't be the solution. And update in place can't change the table structure and though either IF 0 THEN SET DIM; is not required or more likely this code is not going to do what's intended.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Mar 2024 10:59:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Losing-index-when-updating-table-with-hash-replace-option/m-p/919063#M362014</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-03-06T10:59:23Z</dc:date>
    </item>
  </channel>
</rss>

