SAS script to lookup from table based on multiple variables

Reply
New Contributor
Posts: 3

SAS script to lookup from table based on multiple variables

I need a macro or script to perform a lookup from one table to another based on a number of variables. I can't figure out how to do this procedurally. I tried SQL join but the join was too complicated and took too much processing time and based on business logic, the assignment should be done on a if-else logic, assigning the first match, as a user should be assigned the first group_id that they qualify for. 

given lookup_table and facts_table, I need to develop a script to lookup from facts_table to lookup_table, both of which are sas datasets procedurally based on

for each row in facts_table
if facts_table.is_partner = lookup_table.is_partner
and lower(facts_table.product_group) is in lower(lookup_table.product_group)
and lower(facts_table.prod_edtn_short_name) is in lower(lookup_table.prod_edtn_short_name)
and facts_table.item_id is in lookup_table.include_skus
and facts_table.item_id is not in lookup_table.exclude_skus
then assign fact_table group_id = lookup_table.group_id
else try next row in lookup_table against facts_table to see whether all conditions are true.

 

Each row in fact_table is a transaction completed by a customer, so I'm assigning a group_id to each customer transaction based on multiple variables in the fact_table.

 

I have created a sample of facts_table & lookup_table that I have attached here for your reference. Let me know if you have skills and availability for this project.

Valued Guide
Posts: 518

Re: SAS script to lookup from table based on multiple variables

Sound like this can be done fairly easily with a hash lookup. Post a sample of both your facts table and lookup table in the form of a datastep, most users in here will not download data from excel sheets etc

Respected Advisor
Posts: 3,065

Re: SAS script to lookup from table based on multiple variables

[ Edited ]

If your lookup tables are not huge using lookup formats will do what you want and the code is simpler than the hash approach. How big are your lookup tables?

Grand Advisor
Posts: 10,237

Re: SAS script to lookup from table based on multiple variables

Many users here don't want to download Excel files because of virus potential, others have such things blocked by security software. Also if you give us Excel we have to create a SAS data set and due to the non-existent constraints on Excel data cells the result we end up with may not have variables of the same type (numeric or character) and even values.

 

Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

Ask a Question
Discussion stats
  • 3 replies
  • 101 views
  • 0 likes
  • 4 in conversation