<?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 difference between 'replace method' and 'add method' in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/difference-between-replace-method-and-add-method/m-p/239950#M44223</link>
    <description>&lt;P&gt;I don't understand the difference between 'replace method' and 'add method'. The following example give the same output;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;example 1:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data out1;
   length k $8;
   length d $12;

   /* Declare hash object and key and data variable names */
   if _N_ = 1 then do;
      declare hash h();
      rc = h.defineKey('k');
      rc = h.defineData('d');
      rc = h.defineDone();
   end;

   /* Define constant key and data values */
   k = 'Joyce';
   d = 'Ulysses';
   /* Add key and data values to hash object */
   rc = h.replace();
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;example 2&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data out2;
   length k $8;
   length d $12;

   /* Declare hash object and key and data variable names */
   if _N_ = 1 then do;
      declare hash h();
      rc = h.defineKey('k');
      rc = h.defineData('d');
      rc = h.defineDone();
   end;

   /* Define constant key and data values */
   k = 'Joyce';
   d = 'Ulysses';
   /* Add key and data values to hash object */
   rc = h.add();
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 18 Dec 2015 09:36:44 GMT</pubDate>
    <dc:creator>mariopellegrini</dc:creator>
    <dc:date>2015-12-18T09:36:44Z</dc:date>
    <item>
      <title>difference between 'replace method' and 'add method'</title>
      <link>https://communities.sas.com/t5/SAS-Programming/difference-between-replace-method-and-add-method/m-p/239950#M44223</link>
      <description>&lt;P&gt;I don't understand the difference between 'replace method' and 'add method'. The following example give the same output;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;example 1:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data out1;
   length k $8;
   length d $12;

   /* Declare hash object and key and data variable names */
   if _N_ = 1 then do;
      declare hash h();
      rc = h.defineKey('k');
      rc = h.defineData('d');
      rc = h.defineDone();
   end;

   /* Define constant key and data values */
   k = 'Joyce';
   d = 'Ulysses';
   /* Add key and data values to hash object */
   rc = h.replace();
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;example 2&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data out2;
   length k $8;
   length d $12;

   /* Declare hash object and key and data variable names */
   if _N_ = 1 then do;
      declare hash h();
      rc = h.defineKey('k');
      rc = h.defineData('d');
      rc = h.defineDone();
   end;

   /* Define constant key and data values */
   k = 'Joyce';
   d = 'Ulysses';
   /* Add key and data values to hash object */
   rc = h.add();
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Dec 2015 09:36:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/difference-between-replace-method-and-add-method/m-p/239950#M44223</guid>
      <dc:creator>mariopellegrini</dc:creator>
      <dc:date>2015-12-18T09:36:44Z</dc:date>
    </item>
    <item>
      <title>Re: difference between 'replace method' and 'add method'</title>
      <link>https://communities.sas.com/t5/SAS-Programming/difference-between-replace-method-and-add-method/m-p/239959#M44225</link>
      <description>&lt;P&gt;The add method adds entries, and the replace method removes but also adds entries. Maybe this program helps a little:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* Copy-Paste-Problem ..;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;**&amp;nbsp;&amp;amp;colon; is&amp;nbsp;equal to ":", sorry, this never happened before&amp;nbsp;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data A;&lt;BR /&gt;&amp;nbsp; Length N $1.;&lt;BR /&gt;&amp;nbsp; Do N='A','B','C';&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Do X=1 To 5;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Output;&lt;BR /&gt;&amp;nbsp;End;&lt;BR /&gt;&amp;nbsp; End;&lt;BR /&gt;Run;&lt;/P&gt;&lt;P&gt;Data _NULL_;&lt;BR /&gt;&amp;nbsp; Length N $1. X 8;&lt;BR /&gt;&amp;nbsp; Declare Hash H (Dataset:'A',Multidata&amp;amp;colon;'y');&lt;BR /&gt;&amp;nbsp; H.Definekey('N');&lt;BR /&gt;&amp;nbsp; H.Definedata(All:'y');&lt;BR /&gt;&amp;nbsp; H.Definedone();&lt;/P&gt;&lt;P&gt;&amp;nbsp; * This is what add does;&lt;BR /&gt;&amp;nbsp; N='X';X=8;&lt;BR /&gt;&amp;nbsp; rc=H.Add(Key:N,Data&amp;amp;colon;N,Data&amp;amp;colon;X);&lt;BR /&gt;&amp;nbsp; H.Output(Dataset:'B');&lt;/P&gt;&lt;P&gt;&amp;nbsp; * This is what replace does;&lt;BR /&gt;&amp;nbsp; N='B';X=9;&lt;BR /&gt;&amp;nbsp; rc=H.Replace(Key:N,Data&amp;amp;colon;N,Data&amp;amp;colon;X);&lt;BR /&gt;&amp;nbsp; H.Output(Dataset:'C');&lt;BR /&gt;Run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Dec 2015 10:53:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/difference-between-replace-method-and-add-method/m-p/239959#M44225</guid>
      <dc:creator>user24feb</dc:creator>
      <dc:date>2015-12-18T10:53:11Z</dc:date>
    </item>
    <item>
      <title>Re: difference between 'replace method' and 'add method'</title>
      <link>https://communities.sas.com/t5/SAS-Programming/difference-between-replace-method-and-add-method/m-p/240026#M44242</link>
      <description>&lt;P&gt;Not quite. The difference between these 2 methods&amp;nbsp;becomes&amp;nbsp;obvious when the same key exsits in the hash table, otherwise they act the same: adding an entry to the hash table.&lt;/P&gt;
&lt;P&gt;When key is&amp;nbsp;unique, ADD() will not&amp;nbsp;'replace' the existing&amp;nbsp;same key entry in the hash table, so it basically does&amp;nbsp;NOTHING on the same existing key. While REPLACE() will 'replace' the entry when there is a same one.&amp;nbsp;When dup key is allowed,&amp;nbsp;&amp;nbsp;ADD() will add&amp;nbsp;an entry to the same key, REPLACE() will not add an entry to the same key, only to 'replace' all of the entries under the same key.&lt;/P&gt;
&lt;P&gt;Your code has demonstrated the dup key part,&amp;nbsp; so here is an example on unique key.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data A;
	Do X=1 To 5;
		y=999;
		Output;
	End;
Run;

Data _NULL_;
	Length N $1. X 8;
	Declare Hash H (Dataset:'A', ordered:'a');
	H.Definekey('x');
H.Definedata(All:
	'y');
	H.Definedone();

	* This is what add does;
	X=1;
	y=0;
	rc=H.Add();

	* This is what replace does;
	X=2;
	y=0;
	rc=H.Replace();
H.Output(Dataset:
	'C');
Run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Dec 2015 15:55:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/difference-between-replace-method-and-add-method/m-p/240026#M44242</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2015-12-18T15:55:32Z</dc:date>
    </item>
  </channel>
</rss>

