DATA Step, Macro, Functions and more

Strange SAS Hash Object

Accepted Solution Solved
Reply
Contributor
Posts: 24
Accepted Solution

Strange SAS Hash Object

Hi guys,

 

I am trying hard to figure out how this works and I can't seem to manage it. Why do I get any output on the phone and email of Henry using the code below? It seems to me that the hash object doesn't seem to look up the values based on the name and age as specified in the key. Am I doing something wrong?

 

Thank you

 

data H_one;
	length 
		name $10
		age 8
		address $20
	;
	input name $ age address $;

datalines;
John 8 Street_123
John 15 Drive_456
Henry 23 Road_789
run;

data H_two;
	length 
		name $10
		age 8
		phone $10
		email $20
	;
	input name $ age phone $ email $;

datalines;
John 8 1234 abc@abc
John 15 5678 def@def
ABC 25 91011 ghi@ghi
DEF 29 23232 jkl@jkl
run;

data H_three;
	set one;
	length 
		phone $10
		email $20
	;

	if _N_=1 then do;
		declare hash h(dataset:"work.two");
		h.definekey("name","age");
		h.definedata("phone", "email");
		h.definedone();
	end;

	rc = h.find();

run;

 

 


Accepted Solutions
Solution
‎09-04-2016 01:46 PM
Trusted Advisor
Posts: 1,410

Re: Strange SAS Hash Object

You created datasets: H_one, H_two

but in your 3rd step your reference to work.ONE, work.TWO

 

change last step to:

data H_three;	set H_one;	length 
		phone $10
		email $20
	;

	if _N_=1 then do;
		declare hash h(dataset:"work.H_two");
		h.definekey("name","age");
		h.definedata("phone", "email");
		h.definedone();
	end;

	rc = h.find();

run;

 

View solution in original post


All Replies
Solution
‎09-04-2016 01:46 PM
Trusted Advisor
Posts: 1,410

Re: Strange SAS Hash Object

You created datasets: H_one, H_two

but in your 3rd step your reference to work.ONE, work.TWO

 

change last step to:

data H_three;	set H_one;	length 
		phone $10
		email $20
	;

	if _N_=1 then do;
		declare hash h(dataset:"work.H_two");
		h.definekey("name","age");
		h.definedata("phone", "email");
		h.definedone();
	end;

	rc = h.find();

run;

 

Super User
Posts: 9,691

Re: Strange SAS Hash Object

Because phone and email have been retained . Call missing them before h.find().


if _N_=1 then do;
		declare hash h(dataset:"work.two");
		h.definekey("name","age");
		h.definedata("phone", "email");
		h.definedone();
	end;
call missing(phone,email);
	rc = h.find();

Contributor
Posts: 24

Re: Strange SAS Hash Object

Can't believe I did such a stupid mistake, thanks a lot for your support Smiley Happy

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 229 views
  • 0 likes
  • 3 in conversation