I would like to use multiple variables within a WRDS SQL query. I am seeking to search the CRSP database by PERMNO and Date, return the stock price for a particular company on a particular day. I have a large DataFrame from which these variables will be retrieved and used to search the CRSP database.
IN:
sql_query = """
SELECT cusip, permno, date, prc
FROM crsp_a_stock.dsf
WHERE permno = 12490
AND date in %(dates)s
LIMIT 100
"""
parm = {'dates': tuple([line for line in open('dates.txt', 'r')])}
df = db.raw_sql(sql_query, params=parm)
df
OUT:
cusip permno date prc
0 45920010 12490.0 1996-01-31 108.5
This outputs the data for IBM (PERMNO 12490) on the 31/01/1996 as desired. My issue is that I also want to be able to make the permno condition link to a variable, this way I can run the query and retrieve price data for each company on a specific day.
This code is based on this Answer.
Is this possible to do through db.raw_sql? I have searched the WRDS Help Site (specifically the Passing Parameters to SQL section) but have been unable to solve this problem. I am not fussed as to the specifics of the solution (such as using the .txt input file instead of a data frame), I just need to be able to implement variables for multiple WHERE conditions. If it is not possible through db.raw_sql, are there any alternate ways to access the WRDS database through Python that would allow this?
Thanks for any tips or pointers.