09-05-2016 03:52 PM - edited 09-05-2016 03:53 PM
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;
09-05-2016 04:42 PM
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;
09-06-2016 01:09 AM
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?
Need further help from the community? Please ask a new question.