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 🙂

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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