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-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!

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.

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