I am trying to find out tax values for a particular good in a particular city in a particular state. Tax values are in a reference table like this:
state city Good tax
---------------------------------
all all all 0.07
all all chicken 0.04
all jackson all 0.01
arizona all meat 0.02
arizona phoenix meat 0.04
arizona tucson meat 0.03
hawaii all all 0.08
nevada reno cigar 0.11
nevada vegas cigar 0.13
Now lets say if I am looking for tax for (nevada reno cigar) an exact match exists in the reference so the answer is 0.11. But, if I look for (nevada reno chicken) an exact match does not exist, but (all all chicken) can be used as reference and output will be 0.04.
Can you suggest PROC SQL
or match-merge DATA
step logic that handles this situation?
This is a bit long. I use a hash object in these situations. Iteratively "if/then/else" your way through the look up tree attempting to find a value.
I assume Honolulu chicken should be in "Hawaii all chicken" and not "all all chicken."
I included a macro I use for creating the hash object. This uses your data, a set up things to look up and creates and output table with the looked up taxes.
If its something that you only need to do once(i mean not an ongoing process) , then probably a easy way out could be dividing ur dataset into multiple datasets. One dataset would have all observations that have all 'all's in state,observation and good. Another one would have only state or city or good only as All. Another dataset would be a combination of two ALLs in either state/city , city/good or state/good. Making a total of 8 datasets i guess(including a dataset for no Alls in any of the variables. Then when you know which variables has alls , you can merge accordingly . For example - For a dataset with state , city , good u can have a tax of 0.07 without any merge. For a dataset with state and city = 'All' you only need to merge on good. Only other way/option of doing this imo would be to create three new datasets having two variables where var1 = all in all cases and var2 = all city names(multiple obs)/ all state names(multiple obs)/all goods names(multiple obs) and then merge to ur original dataset on var1 to have multiple rows in ur original dataset instead of having ALLs
SQL is ideal the ideal tool to join these tables as it is the most flexible at joining data.
Using DomPazz's test data;
The query below joins each row in the to_look_up table to rows in the taxes table where; state matches or state equals 'all' in the taxes table, city matches or city equals 'all' in the taxes table, and good matches or good equals 'all' in the taxes table.
This can cause more than 1 row in the taxes table to match a row in the to_look_up table. Though we can select the best match by prioritising matches i.e. match state before state equals 'all' and the same for city and good.
The Group By clause is important here. It should be the unique combination of variables in the to_look_up table. With this we can select the best match for each row in the to_look_up table and eliminate all other matches.
Joins similar to this can be used to generate sub-totals in summary tables.