DATA Step, Macro, Functions and more

Hash Object with Wildcards

Accepted Solution Solved
Reply
Contributor
Posts: 24
Accepted Solution

Hash Object with Wildcards

[ Edited ]

Hi All,

 

I have two tables - the first contains products with different attributes and prices. The second table is a mapping of these products based on the attributes they have to different reporting positions. 

 

What I want to do is use a hash object on the mapping table to assign each product to the reportin categories. Would it be possible to use such a hash table if not all products are using the same key - meaning using some kind of wildcards in the mapping table for the attributes the check should skip?

 

For example, in the code below, the mapping is done on the product name and color. This works fine for pencils but i want the ballpens to be reported in category 2.1 no matter the color. Would this be possible with only one hash object and one mapping table?

 

data Products;
	length 
		product $10
		color $10
		price 8
	;
	input product $ color $ price;

datalines;
Pencil Blue 10
Pencil Red 15
BallPoint Blue 20
BallPoint Red 25
run;

data Mapping;
	length 
		product $10
		color $10
		report_position $10
	;
	input product $ color $ report_position $;

datalines;
Pencil Blue 1.1.1
Pencil Red 1.1.2
BallPoint * 2.1
run;

data report;
	set Products;
	length 
		report_position $10
	;

	if _N_=1 then do;
		declare hash h(dataset:"Mapping");
		h.definekey("product","color");
		h.definedata("report_position");
		h.definedone();
	end;

	rc = h.find();

run;

Thank you?


Accepted Solutions
Solution
‎09-08-2016 01:12 AM
SAS Super FREQ
Posts: 709

Re: Hash Object with Wildcards

They key has to be a match. You mentioned "for the code below", but there is no code.

View solution in original post


All Replies
SAS Super FREQ
Posts: 709

Re: Hash Object with Wildcards

Hi

 

If the find method does not find an entry, you could find again this time, setting the color to search for to "*", see code sample below.

The hash object always needs all the keys to be present.

 


data report;
  set Products;
  length 
    report_position $10
  ;

  if _N_=1 then do;
    declare hash h(dataset:"Mapping");
    h.definekey("product","color");
    h.definedata("report_position");
    h.definedone();
  end;

  rc = h.find();
  if rc ne 0 then do;
    rc = h.find(key: product, key: "*");
  end;
  putlog _all_;

run;

Bruno

Contributor
Posts: 24

Re: Hash Object with Wildcards

Posted in reply to Bruno_SAS

Thanks a lot for the help.

 

One additional question - maybe also stupid - is there a way to specify for the key a NOT value. 

 

For example, for the code below, is it possible specifiy a mapping for the pencils which are NOT blue? If not via the hash object, maybe you have some other sugestions how to do this in an optimal way?

 

Thank you.

Solution
‎09-08-2016 01:12 AM
SAS Super FREQ
Posts: 709

Re: Hash Object with Wildcards

They key has to be a match. You mentioned "for the code below", but there is no code.
Contributor
Posts: 24

Re: Hash Object with Wildcards

Posted in reply to Bruno_SAS
Sorry, I meant the code above ... Smiley Happy

However you replied to my question so thank you very much!
☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 321 views
  • 0 likes
  • 2 in conversation