BookmarkSubscribeRSS Feed
superjohn
Calcite | Level 5

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.

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

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

SASKiwi
PROC Star

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?

ballardw
Super User

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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