SAS Procedures

Help using Base SAS procedures
BookmarkSubscribeRSS Feed
sassharp
Calcite | Level 5

need to incorporate 'NODUPKEY' in hash short as in proc sort nodupkey. Any examples?

7 REPLIES 7
Haikuo
Onyx | Level 15

data have;

input id  var @@;

cards;

1 2 1 3 10 6 4 5 10 7 8 2

;

data _null_;

  if _n_=1 then do;

dcl hash h( ordered: 'a');

h.definekey('id');

h.definedata('id','var');

h.definedone();

   end;

   do until (done);

   set have end=done;

   _rc=h.add();

      end;

h.output(dataset:'want');

   stop;

   run;

Regards,

Haikuo

FriedEgg
SAS Employee

The hash object by default drops duplicate keys.  This is controlled by the duplicate option which tells SAS whether to replace or error when a duplicate key is encountered (when loading a dataset into a hash using the dataset option).  To avoid this use the multidata:'y' option in the declare statement.

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002576871.htm

Check out the post by MikeZdeb in the link for a great list of reference papers for learning about Hash objects in SAS:

http://communities.sas.com/thread/33376?tstart=0

Haikuo
Onyx | Level 15

Reminded by FriedEgg's comments "The hash object by default drops duplicate keys.", add() method is not needed for this purpose if choose to load the dataset once for all. However, if chose the last record when duplicating, replace() may still be needed:

data have;

input id  var @@;

cards;

1 2 1 3 10 6 4 5 10 7 8 2

;

   data _null_;

  set have (obs=1);

dcl hash h( dataset: 'have', ordered: 'a');

h.definekey('id');

h.definedata('id','var');

h.definedone();

h.output(dataset:'want');

   run;

This one feels better.

Regards,

Haikuo

FriedEgg
SAS Employee

I may not have made myself clear enough:

keep the first incidence of key

dcl hash h(dataset:'have', ordered:'a');

keep the last incidence of key

dcl hash h(dataset:'have', ordered:'a' ,duplicate:'r');

keep all incidience of key

dcl hash h(dataset:'have', ordered:'a' ,multidata:'y');

fail if duplicates of key exist in loading file

dcl hash h(dataset:'have', ordered:'a' ,duplicate:'e');

Haikuo
Onyx | Level 15

Learned and Agreed!

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 2731 views
  • 1 like
  • 3 in conversation