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.
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
.
in between REPLACE() and ADD() methods, REPLACE() is the one that will produce different outcome if you have duplicated keys.
Haikuo
Ok, thanks. Yes, I was wondering about the output, not the performance.
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.
Thanks.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
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.
Ready to level-up your skills? Choose your own adventure.