DATA Step, Macro, Functions and more

HASH object key replacement

Reply
Contributor
Posts: 34

HASH object key replacement

Hello,
I am trying to load a dataset into a hash object.
I want to use the 'DUPLICATE' option in the declare statement to replace all the duplicate keys when loading from the dataset.

But it gives me a warning: "Unrecognized or missing argument tag"
all other options in the declare statement like ORDERED work fine.
I am running this program on SAS learning guide 4.1

why is that it is not recognizing the DUPLICATE option?
Here's the code:

OPTIONS formdlim='#';
data a;
input key a b c;
datalines;
1 10 100 9
1 20 200 8
2 20 100 8
3 30 100 9
4 40 100 9
4 20 200 8
;

Data _null_;
If _N_ = 1 then do;
Declare hash H(dataset: 'work.a',duplicate:'replace', ordered:'d');
H.definekey('key');
H.definedata('a','b','c');
H.definedone();
Call missing (key,a,b,c);
end;

rc=H.OUTPUT(dataset:"work.out");
run;

proc print data =work.out;
run;

Any help on this is appreciated.
Thanks,
SAPPER
Super Contributor
Posts: 474

Re: HASH object key replacement

Are we talking about a SAS 9.2 installation?

Because this feature is not available prior that version.

Cheers from Portugal.

Daniel Santos @ www.cgd.pt
Contributor
Posts: 34

Re: HASH object key replacement

Hi Daniel,
thanks for the reply.
I am running this on 9.1.3.
Guess that explains the problem

Thanks
SAPPER.
SAS Employee
Posts: 39

Re: HASH object key replacement

Even though the duplicate: initialization option isn't available in SAS 9.1.3, you can load the hash object with the last obs from a group of duplicate keys. To do this, use the REPLACE method. Here is a small program:

[pre]
data a;
input key a b c;
datalines;
1 10 100 9
1 20 200 8
2 20 100 8
3 30 100 9
4 40 100 9
4 20 200 8
;

data _null_;
if _N_ = 1 then do;
declare hash H(ordered:'d');
H.definekey('key');
H.definedata('a','b','c');
H.definedone();

/* Load the hash object, getting last obs when there */
/* are duplicate keys. */
do while (^done);
set a end=done;
H.replace();
end;
end;

rc=H.output(dataset:"work.out");

stop;
run;

proc print data =work.out;
run;
[/pre]
Contributor
Posts: 34

Re: HASH object key replacement

Thanks Jason.
Ask a Question
Discussion stats
  • 4 replies
  • 305 views
  • 0 likes
  • 3 in conversation