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 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?

1 ACCEPTED SOLUTION

Accepted Solutions
BrunoMueller
SAS Super FREQ
They key has to be a match. You mentioned "for the code below", but there is no code.

View solution in original post

4 REPLIES 4
BrunoMueller
SAS Super FREQ

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

_SAS_
Obsidian | Level 7

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.

BrunoMueller
SAS Super FREQ
They key has to be a match. You mentioned "for the code below", but there is no code.
_SAS_
Obsidian | Level 7
Sorry, I meant the code above ... 🙂

However you replied to my question so thank you very much!

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
  • 4 replies
  • 1577 views
  • 0 likes
  • 2 in conversation