BookmarkSubscribeRSS Feed
jpm2478
Calcite | Level 5

Hi,

 

I'm using Hash Object and I'm trying to get the Output to overwrite the initial dataset (Y). I'm using  if last then h.output(dataset:'work.y');

where work.Y is an initial dataset.

 

When I run this code the Output is not shown in Work.Y but when I use a newdataset name like  if last then h.output(dataset:'AA'); the output is shown in new dataset AA.

 

How do I get Hash Output to be shown on the Initial Dataset.

5 REPLIES 5
SuryaKiran
Meteorite | Level 14

Are you trying to update a dataset using the values from other dataset? 

 

Try this if so:

data have;  
  if _n_=1 then do;
    declare hash ud(dataset:'update');
    ud.defineKey('id');
    ud.defineData('val1', 'val2');
    ud.defineDone();
  end;
  modify have;
  rcUpdate = ud.find();
  if rcUpdate=0 then replace;
run;
Thanks,
Suryakiran
novinosrin
Tourmaline | Level 20

Hi @jpm2478 If your question relates to the thread https://communities.sas.com/t5/SAS-Procedures/Compare-two-datasets-using-Hash-and-update-values/m-p/...

 

You can try options 1. modify statement, 2. @ballardw 's non hash solution among others etc

 

Of course if you are comfortable with hashes, by all means do explore. Hash understanding is tricky for the reason understanding 

 

PDV::Memory table   key value pair matches can be confusing and pulling from Memory table ::  PDV along with it can be daunting. But  totally up to you

mkeintz
PROC Star

Are you trying to use hash output method to overwrite a data set that your are reading from in the same data step?   I don't believe you can do that.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
novinosrin
Tourmaline | Level 20

@mkeintz Sir, I believe that's exactly OP's req. I wish I was brilliant using modify statement and automatic vars associated with it like you demonstrated in one your posts which I regret I failed to add to my notes. If i had it, I would have plagiarized it and demonstrated here3

mkeintz
PROC Star

@novinosrin

 

You may be referring to Update and replace data in a table, but that problem was only to decide whether to replace the last record in the master, or to append the record.  I think this problem has to do with multiple transaction/update records.  In which case @SuryaKiran's solution seems apt - as long as the UPDATE dataset has only one record per id.

 

edited revision:  ... and also assuming that there are no new id's in UPDATE.

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 5 replies
  • 1227 views
  • 0 likes
  • 4 in conversation