BookmarkSubscribeRSS Feed
theponcer
Quartz | Level 8

I am trying to use a hash table to create a table, output it, and then set it back inside the data table and continue processing. However, the set statement executes at compilation, and the hash code executes at run time. This means I am trying to set a table that doesn't yet exist. Is there a way around this, or am I stuck? Here is my example code:

 

data person;
   input name $ dept $ salary 8.;
   datalines;
John Sales 100000
Mary Acctng 100000
Teresa Sales 100000
;

data dept;
   input dept $;
   datalines;
Sales 
;

data test;
set person;

if 0 then set dept;

if _n_=1 then do;

dcl hash id(dataset:'dept');
id.definekey('dept');
id.definedata('dept');
id.definedone();

dcl hash clones();
clones.definekey('name','dept','salary');
clones.definedata('name','dept','salary');
clones.definedone();

end;

rc_id=id.find();

if rc_id = 0 then 
clones.add();

clones.output(dataset:'work.clones');
/*set work.clones;*/
if name = "Teresa" then salary = salary +10000;
run;

data want_test;
   input name $ dept $ salary 8.;
   datalines;
John Sales 100000
Mary Acctng 100000
Teresa Sales 100000
John Sales 100000
Teresa Sales 110000
;

data want_clones;
   input name $ dept $ salary 8.;
   datalines;
John Sales 100000
Teresa Sales 100000
;

 

 

5 REPLIES 5
Patrick
Opal | Level 21

Plus the hash output method writes the whole hash table to a SAS table and though should only be called once.

So yes, you've got it already: What you're trying to do here can't work. But why would you need such a Set statement in first place? You've got already the hash table so just retrieve from there whatever you need. I haven't really understood what you're trying to achieve so can't be more concrete.

 

Correction:

Using open=defer in the set statement you can write to a hash table and read it with a set statement within the same data step. 

See discussion below.

theponcer
Quartz | Level 8

I'm basically trying to write out certain records to a separate table (thus the hash table), duplicate the records in the data table, and then continue processing them. What I've been able to find about duplicating records is essentially, "use the output statement", but if I do this I can no longer process the records as they have already been written out to the table. 

hashman
Ammonite | Level 13

@theponcer:

OPEN=DEFER, Roger DeAngelis, Richard DeVenezia, and SAS-L in general are your friends:

 

https://listserv.uga.edu/cgi-bin/wa?A2=SAS-L;dadc0d11.1909d

 

Kind regards

Paul D. 

Patrick
Opal | Level 21

@hashman 

That's wicked! Thanks for sharing.

hashman
Ammonite | Level 13

@Patrick:

Welcome. SAS-L is nowhere near what it used to be volume-wise, but the quality of its elite is still there.  

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 1459 views
  • 2 likes
  • 3 in conversation