BookmarkSubscribeRSS Feed
evp000
Quartz | Level 8

Hi,

In Art Carpenter's Innovative SAS Techniques, Chapter 3, section 3.3.7 there is the following code:

data trans;

     length lname $10 fname $6 sex $1;

     fname = 'Mary'; lname = 'Adams'; sex = 'N'; output;

     fname = 'Joan'; lname = 'Adamson'; sex = 'x'; output;

     fname = 'Peter'; lname = 'Anla'; sex = 'A'; output;

run;

data newdemog (drop = rc);

     declare hash upd(hashexp:10);

     upd.definekey('lname','fname');

     upd.definedata('sex');

     upd.definedone();

     do until(lasttrans);

          set trans end=lasttrans;

          rc=upd.add();

     end;

     do until(lastdemog);

          set advrtp.demog end=lastdemog;

          rc=upd.find();

          output newdemog;

     end;

     stop;

run;

Was it necessary to have the first loop to populate the hash table, or would it have been the same to just declare it with

declare hash upd (dataset: 'trans');  ?

Does this code do the same thing (without do loops)?

data newdemog (drop = rc);

     if 0 then set advrtp.demog trans ;

     if _n_ = 1 then

     do;

          declare hash upd(hashexp:10 dataset: 'trans' );

          upd.definekey('lname','fname');

          upd.definedata('sex');

          upd.definedone();

     end;

     set advrtp.demog;

     call missing(sex);

     rc = upd find();

     output;

run;

Thanks for your input.

4 REPLIES 4
Haikuo
Onyx | Level 15

When asking if they are the same, you are implying several things at the same time, for example:

1. Will they get the same outcome? Yes, I believe so.

2.Will they get the same/similar performance/efficiency? I have no clue. You will have to benchmark it yourself Smiley Happy.

in between REPLACE() and ADD() methods, REPLACE() is the one that will produce different outcome if you have duplicated keys.

Haikuo

evp000
Quartz | Level 8

Ok, thanks.  Yes, I was wondering about the output, not the performance. 

gergely_batho
SAS Employee

Yes, the two codes produce the same output (though I have not tested).

I think in older versions of SAS there was no dasaset argument tag.

evp000
Quartz | Level 8

Thanks.

SAS Innovate 2025: Register Today!

 

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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
  • 4 replies
  • 1078 views
  • 7 likes
  • 3 in conversation