DATA Step, Macro, Functions and more

HASH Left Join

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

HASH Left Join

[ Edited ]

Hello,

 

I need help with joining two tables with hash. Both tables has one unique key and one other duplicate variable. Below are the matching variables

x1 - y1

x15 - y17

 

/* 15 variables */

Table a 

x1

.

.

x15

 

/* 17 variables */

Table b

y1

.

.

y17

 

I tried with following code but I am getting all the variables from table b and only one from table a instead I want to display all the var from table a and all the var from table b

 

 

data left_join(drop=rc);

	declare Hash a (dataset: "o.a");
	rc=a.definekey('x1');
	rc=a.definedata();
	rc=a.definedone();

	do until(eof);
	 set o.b end=eof;
	 rc=a.find();
	 output;
	end;
	stop;
run;

 

 

 


Accepted Solutions
Solution
‎06-01-2017 03:26 PM
Valued Guide
Posts: 797

Re: HASH Left Join

[ Edited ]

Ah yes,  the OP said it's many-to-one but my program assumed unique X1 values in A.  Here's the code for duplicated X1 values:

 

data want;
  if _n_=1 then do;
    if 0 then set a;
    declare hash right (dataset:'a',multidata:'Y');
       right.definekey('x1');
       right.definedata(all:'Y');
       right.definedone();
       right.add();   /* Add a dummy record with all missing vals*/
  end;

  set b;  /* left */ 

  rc=right.find(key:y1); 
if rc^=0 then rc=right.find(key:.); do while (rc=0); output; rc=right.find_next(); end; run;

 

Edited at 6/1/2017 14:58 UTC-5:00

View solution in original post


All Replies
Super User
Posts: 6,928

Re: HASH Left Join

I guess you meant that x15 should match with y17, as there are only fifteen variables in dataset a?

 

That said, what is the relationship between the tables with regard to the key variables? Is it one-to-many, or many-to-many?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
New Contributor
Posts: 3

Re: HASH Left Join

No x15 doesnt have to match y17. x1 should mach y1. It is one to many relationship.

Regular Contributor
Posts: 213

Re: HASH Left Join

Check this paper Using the SAS® Hash Object with Duplicate Key Entries

There is section "2. One-to-Many, Many-to-Many Left Joins" might have the answer you are looking for.

 

Hope this helps,

Ahmed

Super User
Posts: 6,928

Re: HASH Left Join


kpdoe wrote:

No x15 doesnt have to match y17. x1 should mach y1. It is one to many relationship.


Then my preferred method is

proc sort data=a;
by x1;
run;

proc sort data=b;
by y1;
run;

data want;
merge
  a (in=a)
  b (in=b rename=(y1=x1))
;
by x1;
run;

You can add a subsetting if that selects if you want an inner, left or right join.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Valued Guide
Posts: 797

Re: HASH Left Join

Just one caveat: this can do left, right or inner, but not many-to-many.

Valued Guide
Posts: 797

Re: HASH Left Join

[ Edited ]

 

The problem with


 

 

data left_join(drop=rc);

	declare Hash a (dataset: "o.a");
	rc=a.definekey('x1');
	rc=a.definedata();
	rc=a.definedone();

	do until(eof);
	 set o.b end=eof;
	 rc=a.find();
	 output;
	end;
	stop;
run;

 

 

 


 

  1. Your a.definedata probably should be a.definedata(all:'Y').  That's probably why you are not getting all the A vars
  2. You're apparently simulating B left join A on b.y1=a.x1.  Your a.find() default to X1 as the lookup key, but you want Y1 to be the lookup key:
       try a.find(key:y1).
  3. In your loop, when a B is NOT found in A, all the vars in A should be set to missing, but you will not get that.  Instead you will get all the A vars in the most recent successful FIND method.

 

data want;
  if _n_=1 then do;
    if 0 then set a;
    declare hash right (dataset:'a');
       right.definekey('x1');
       right.definedata(all:'Y');
       right.definedone();
       right.add();   /* Add a dummy record with all missing vals*/
  end;

  set b;  /* left */ 

  rc=right.find(key:.);
rc=right.find(key:y1); run;

 

Notes:

  1. I added a row of missing value to the hash object based on dataset A.
  2. I use that row (rc=right.find(key:.) to set all A vars to missing prior to searching for A.X1=B.Y1.  If that search is not successful it won't inadvertantly inherit A vars from a prior successfull search.
  3. Editted addition: This assumes that X1 is unique in dataset A.
Solution
‎06-01-2017 03:26 PM
Valued Guide
Posts: 797

Re: HASH Left Join

[ Edited ]

Ah yes,  the OP said it's many-to-one but my program assumed unique X1 values in A.  Here's the code for duplicated X1 values:

 

data want;
  if _n_=1 then do;
    if 0 then set a;
    declare hash right (dataset:'a',multidata:'Y');
       right.definekey('x1');
       right.definedata(all:'Y');
       right.definedone();
       right.add();   /* Add a dummy record with all missing vals*/
  end;

  set b;  /* left */ 

  rc=right.find(key:y1); 
if rc^=0 then rc=right.find(key:.); do while (rc=0); output; rc=right.find_next(); end; run;

 

Edited at 6/1/2017 14:58 UTC-5:00

New Contributor
Posts: 3

Re: HASH Left Join

Thank you very much. It worked!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 191 views
  • 0 likes
  • 4 in conversation