Swati, this a a great way to learn Python, I hope that my answer helps.
Background
First, the data structure that best resembles a SAS dataset is the Pandas DataFrame. If you have not installed the Pandas library, I strongly encourage you to do so and follow these examples.
Second, I assume that the table 'one' is already a Pandas DataFrame. If that is not a helpful assumption, you may need to see code to import SAS datasets, assign file paths, or connect to a database, depending on your data management choices.
Also, here is my interpretation of your code:
%macro bank_data(var,month);
proc sql;
create table work.work_&var. as
select a.accountid,
a.customerseg,
a.product,
a.month,
b.deliquency_bucket
from work.one as a
left join mibase.corporate_&month. as b
on a.accountid = b.accountid
and a.month = b.&month;
quit;
%mend;
Pandas Merge
Generally, to do a left join in Pandas, use Dataframe.merge(). If the left table is called "one" and the right table is called "corporate_month", then the merge statement looks as follows. The argument left_on
applies to the left-dataset "one" and the right_on
argument applies to the right-dataset "corporate_month".
month = 202010
corporate_month = 'corporate_{}.sas7bdat'.format(month)
work_var = one.merge(right=corporate_month, how='left', left_on=['accountid', 'month'], right_on=['accountid', month])
Dynamic Variable Assignment
Now, to name the resulting dataset based on a variable. SAS Macro's are simply text replacement, but you cannot use that concept in variable assignment in Python. Instead, if you insist on doing this, you will need to get comfortable with dictionaries. Below is how I would implement your requirement.
var = 1
month = 202010
dict_of_dfs = {}
corporate_month = 'corporate_{}.sas7bdat'.format(month)
work_var = 'work_{}'.format(var)
dict_of_dfs[work_var] = one.merge(right=corporate_month, how='left', left_on=['accountid', 'month'], right_on=['accountid', month])
As a Function
Lastly, to turn this into a function where you pass "var" and "month" as arguments:
dict_of_dfs = {}
def bank_data(var, month):
corporate_month = 'corporate_{}.sas7bdat'.format(month)
work_var = 'work_{}'.format(var)
dict_of_dfs[work_var] = one.merge(right=corporate_month, how='left', left_on=['accountid', 'month'], right_on=['accountid', month])
bank_data(1, 202010)
bank_data(2, 202011)
bank_data(3, 202012)
Export
If you want to export each of the resulting tables as SAS datasets, look into the SASPy library.