DATA Step, Macro, Functions and more

difference between 'replace method' and 'add method'

Accepted Solution Solved
Reply
Contributor
Posts: 37
Accepted Solution

difference between 'replace method' and 'add method'

I don't understand the difference between 'replace method' and 'add method'. The following example give the same output;

 

example 1:

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;

example 2

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;

 


Accepted Solutions
Solution
‎12-18-2015 01:52 PM
Respected Advisor
Posts: 3,156

Re: difference between 'replace method' and 'add method'

Posted in reply to user24feb

Not quite. The difference between these 2 methods becomes obvious when the same key exsits in the hash table, otherwise they act the same: adding an entry to the hash table.

When key is unique, ADD() will not 'replace' the existing same key entry in the hash table, so it basically does NOTHING on the same existing key. While REPLACE() will 'replace' the entry when there is a same one. When dup key is allowed,  ADD() will add 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.

Your code has demonstrated the dup key part,  so here is an example on unique key.

 

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;

View solution in original post


All Replies
Super Contributor
Posts: 340

Re: difference between 'replace method' and 'add method'

[ Edited ]
Posted in reply to mario_pellegrini

The add method adds entries, and the replace method removes but also adds entries. Maybe this program helps a little:

 

* Copy-Paste-Problem ..;

** : is equal to ":", sorry, this never happened before ;

 

Data A;
  Length N $1.;
  Do N='A','B','C';
    Do X=1 To 5;
      Output;
 End;
  End;
Run;

Data _NULL_;
  Length N $1. X 8;
  Declare Hash H (Dataset:'A',Multidata:'y');
  H.Definekey('N');
  H.Definedata(All:'y');
  H.Definedone();

  * This is what add does;
  N='X';X=8;
  rc=H.Add(Key:N,Data:N,Data:X);
  H.Output(Dataset:'B');

  * This is what replace does;
  N='B';X=9;
  rc=H.Replace(Key:N,Data:N,Data:X);
  H.Output(Dataset:'C');
Run;

 

Solution
‎12-18-2015 01:52 PM
Respected Advisor
Posts: 3,156

Re: difference between 'replace method' and 'add method'

Posted in reply to user24feb

Not quite. The difference between these 2 methods becomes obvious when the same key exsits in the hash table, otherwise they act the same: adding an entry to the hash table.

When key is unique, ADD() will not 'replace' the existing same key entry in the hash table, so it basically does NOTHING on the same existing key. While REPLACE() will 'replace' the entry when there is a same one. When dup key is allowed,  ADD() will add 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.

Your code has demonstrated the dup key part,  so here is an example on unique key.

 

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;
🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 226 views
  • 1 like
  • 3 in conversation