BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
windlove
Fluorite | Level 6

I have a lookup table to be used for multiple datasets. 

 

How can I use hash table method to perform subset and output for each of them in the same step?

 

I have experimented the following, but clearly it is not working. 

 

 

data outA outB;
	
    if 0 then set lookup(keep=ppn);
    if _n_ = 1 then do;
        declare hash hash(dataset: 'lookup(keep=ppn)');
hash.defineKey('ppn'); hash.defineDone(); end; do until(last); set tableA end = last; if ~hash.find() then output outA; end; do until(last); set tableB end = last; if ~hash.find() then output outB; end; run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

The main thing is to reset variable LAST before the 2nd do-while loop.

I would use the check() method because you don't need to read the value from the hash table into the PDV. You just want to check for existence.

I've never seen the ~ used in front of the hash reference. It appears to work but can you explain to me what's that for?

 

Here your code amended - inclusive a few cosmetic changes.

data tableA tableB lookup;
  set sashelp.class;
  ppn=name;
  output tableA;
  output tableB;

  if _n_<4 then
    output lookup;
run;

data outA outB;
  if 0 then set lookup(keep=ppn);

  declare hash h1(dataset: 'lookup(keep=ppn)');
  h1.defineKey('ppn');
  h1.defineDone();

  do until(last);
    set tableA end = last;
    if h1.check()=0 then output outA;
  end;

  last=0;
  do until(last);
    set tableB end = last;
    if h1.check()=0 then output outB;
  end;

  stop;
run;

View solution in original post

5 REPLIES 5
Patrick
Opal | Level 21

The main thing is to reset variable LAST before the 2nd do-while loop.

I would use the check() method because you don't need to read the value from the hash table into the PDV. You just want to check for existence.

I've never seen the ~ used in front of the hash reference. It appears to work but can you explain to me what's that for?

 

Here your code amended - inclusive a few cosmetic changes.

data tableA tableB lookup;
  set sashelp.class;
  ppn=name;
  output tableA;
  output tableB;

  if _n_<4 then
    output lookup;
run;

data outA outB;
  if 0 then set lookup(keep=ppn);

  declare hash h1(dataset: 'lookup(keep=ppn)');
  h1.defineKey('ppn');
  h1.defineDone();

  do until(last);
    set tableA end = last;
    if h1.check()=0 then output outA;
  end;

  last=0;
  do until(last);
    set tableB end = last;
    if h1.check()=0 then output outB;
  end;

  stop;
run;
windlove
Fluorite | Level 6

Thanks Patrick. 

 

"~hash.check" is the same as "hash.check = 0".

 

This is what I learnt when someone taught me hash table method for the first time. Then got used to it.  

 

Your solution partially worked. But both data were merged. 

 

Is there anyway I can prevent them from merging?

Patrick
Opal | Level 21

What do you mean by merging? 

 

It's certainly not merging rows but it will add all the variables from the source tables to the PDV and though to the target tables. You can use a data set KEEP option to prevent this from happening.

data outA(keep=<variable list>) outB(keep=<variable list>);

 

windlove
Fluorite | Level 6

You are absolutely right. Thank you. 

FreelanceReinh
Jade | Level 19

@Patrick wrote:

I've never seen the ~ used in front of the hash reference. It appears to work but can you explain to me what's that for?


It's just one of the (operating system dependent) symbols for the logical operator NOT, by no means specific to hash object syntax. (Strangely enough, the linked documentation seems to contain a typo: ° instead of ^ as it's correctly listed in WHERE syntax.)

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

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
  • 5 replies
  • 1188 views
  • 1 like
  • 3 in conversation