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

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;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

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

3 REPLIES 3
Shmuel
Garnet | Level 18

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;

 

Ksharp
Super User
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();

_SAS_
Obsidian | Level 7

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

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
  • 3 replies
  • 1408 views
  • 0 likes
  • 3 in conversation