BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

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

2 REPLIES 2
user24feb
Barite | Level 11

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;

 

Haikuo
Onyx | Level 15

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 1242 views
  • 2 likes
  • 3 in conversation