-1

I have a bit of a problem that I'm having trouble solving. My company has a bunch of legacy SAS code that they're converting to a combination of Python and SQL. And there is a merge in some Proc SQL code that I can't figure out how to correctly translate to Python. Here is the code:

proc sql;
select
... (skipping through irrelevant parts)
from main_data
left join joined_data on main_data.account = joined_data.account
 AND joined_data.ID ne ''
 and ((joined-data.source in ('option1','option2')
   and main_data.num in ('123','456','789'))
   or (joined_data.source in ('option3') and main_data.num in ('101112')))

Keep in mind that the data will be stored in pandas data frames. The first part of the join (account=account) is easy to do through pd.merge, but I'm a bit stumped on how to mimic the second part in python.

Thanks for the help!

Tom
  • 47,574
  • 2
  • 16
  • 29
  • Watch out for the not equal to blank test. Python will probably use tri-level logic when there are null/missing/NA values in your data. SAS code only use binary logic, True or False, even when one or more of the values is missing. – Tom Jun 30 '23 at 13:58
  • Second part are just filters on the joins. You can use filter operations on the joined data frame to reduce the output though it's a complex filter. – Reeza Jun 30 '23 at 16:08
  • @Reeza, that seems right, but I'm a bit stumped on how exactly to do the "or" section at the end. I guess I could create a bunch of indicator variables and do it over multiple left merges? – N0DuckingWay Jul 01 '23 at 01:27

0 Answers0