Convert WRDS or local SAS data to PostgreSQL, parquet, or CSV.
Project description
Library to convert WRDS SAS data
This package was created to convert WRDS SAS data to modern data formats. This package has three major functions, one for each of three popular data formats.
wrds_update()
: Imports WRDS SAS data into a PostgreSQL database.wrds_update_pq()
: Converts WRDS SAS data to parquet files.wrds_update_csv()
: Converts WRDS SAS data to gzipped CSV files.
This package was primarily designed to handle WRDS data, but some support is provided for importing a local SAS file (*.sas7dbat
) into a PostgreSQL database.
Requirements
1. Python
The software uses Python 3 and depends on Pandas, SQLAlchemy and Paramiko. In addition, the Python scripts generally interact with PostgreSQL using SQLAlchemy and the Psycopg library. These dependencies are installed when you use Pip (see instructions below).
2. A WRDS ID
To use public-key authentication to access WRDS, follow hints taken from here to set up a public key.
Copy that key to the WRDS server from the terminal on your computer.
(Note that this code assumes you have a directory .ssh
in your home directory. If not, log into WRDS via SSH, then type mkdir ~/.ssh
to create this.)
Here's code (for me) to create the key and send it to WRDS:
ssh-keygen -t rsa
cat ~/.ssh/id_rsa.pub | ssh iangow@wrds-cloud.wharton.upenn.edu "cat >> ~/.ssh/authorized_keys"
Use an empty passphrase in setting up the key so that the scripts can run without user intervention.
3. PostgreSQL
For the wrds_update()
function, you should have write access to a PostgreSQL database to store the data.
4. Environment variables
Environment variables that the code can use include:
PGDATABASE
: The name of the PostgreSQL database you use.PGUSER
: Your username on the PostgreSQL database.PGHOST
: Where the PostgreSQL database is to be found (this will belocalhost
if its on the same machine as you're running the code on)WRDS_ID
: Your WRDS ID.DATA_DIR
: The local repository for parquet files.CSV_DIR
: The local repository for compressed CSV files.
Once can set these environment variables in (say) ~/.zprofile
:
export PGHOST="localhost"
export PGDATABASE="crsp"
export WRDS_ID="iangow"
export PGUSER="igow"
Using the function wrds_update
.
Two arguments table_name
and schema
are required.
1. WRDS Settings
Set WRDS_ID
using either wrds_id=your_wrds_id
in the function call or the environment variable WRDS_ID
.
2. Environment variables
The wrds_udpate()
function will use the environment variables PGHOST
, PGDATABASE
, and PGUSER
if you If you have set them. Otherwise, you need to provide values as arguments to wrds_udpate()
. Default PGPORT
is5432
.
3. Table settings
To tailor your request, specify the following arguments:
fix_missing
: set toTrue
to fix missing values. This addresses special missign values, which SAS'sPROC EXPORT
dumps as strings. Default value isFalse
.fix_cr
: set toTrue
to fix characters. Default value isFalse
.drop
: add column names to be dropped (e.g.,drop="id name"
will drop columnsid
andname
).obs
: add maxium number of observations (e.g.,obs=10
will import the first 10 rows from the table on WRDS).rename
: rename columns (e.g.,rename="fee=mngt_fee"
renamesfee
tomngt_fee
).force
: set toTrue
to force update. Default value isFalse
.
Importing SAS data into PostgreSQL
The software can also upload SAS file directly to PostgreSQL.
You need to have local SAS in order to use this function.
Use fpath
to specify the path to the file to be imported
Examples
Here are some examples.
If you are at the home directory of this git repo, you can import and use the software as shown below.
To install it from Github:
sudo -H pip3 install git+https://github.com/iangow/wrds2pg --upgrade
This software is also available from PyPI. To install it from PyPI:
pip3 install wrds2pg
Example usage:
from wrds2pg import wrds_update
# 1. Download crsp.mcti from wrds and upload to pg as crps.mcti
# Simplest version
wrds_update(table_name="mcti", schema="crsp")
# Tailored arguments
wrds_update(table_name="mcti", schema="crsp", host=your_pghost,
dbname=your_pg_database,
fix_missing=True, fix_cr=True, drop="b30ret b30ind", obs=10,
rename="caldt=calendar_date", force=True)
# 2. Upload test.sas7dbat to pg as crsp.mcti
wrds_update(table_name="mcti", schema="crsp", fpath="your_path/test.sas7dbat")
Report bugs
Author: Ian Gow, iandgow@gmail.com Contributors: Jingyu Zhang, jingyu.zhang@chicagobooth.edu, Evan Jo.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file wrds2pg-1.0.34.tar.gz
.
File metadata
- Download URL: wrds2pg-1.0.34.tar.gz
- Upload date:
- Size: 15.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d2dc8e2bfc20e88fc86b9ef0ea156dcafebc85be00eb5e79cc469ab5254390c |
|
MD5 | e54ebb4f9ff7284b6d0113fb4b20fa67 |
|
BLAKE2b-256 | fd88dbc48fb8bf6c91971feda68c7cb99db1b966c561cab792c303a2809fdb1d |
File details
Details for the file wrds2pg-1.0.34-py3-none-any.whl
.
File metadata
- Download URL: wrds2pg-1.0.34-py3-none-any.whl
- Upload date:
- Size: 13.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ef826f9a0ba5090ea0b184ab44fdf2c64096f410cd3d5b6755a6a89cb7096b6e |
|
MD5 | a449b006b3c6cb60c04c6b6572750a04 |
|
BLAKE2b-256 | a26084bfe64890bf01ddd6651cb8663ac35ecb6b482126655d8af7de89cec022 |