Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

streamline connection to WRDS in "connect_WRDS.R" #15

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions code/R/connect_WRDS.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# --- Header -------------------------------------------------------------------
# See LICENSE file for details
#
# This code establishes a connection to WRDS
# ------------------------------------------------------------------------------
#
#
library(RPostgres)
library(DBI)
#
#
message("Trying to establish connection to WRDS")
#
#
# --- WRDS Credentials ----------------------------------------------------------
if(file.exists("config.csv")){
df <- readr::read_csv("config.csv", col_type = readr::cols(), comment = "#")
cfg <- as.list(df$value)
names(cfg) <- df$variable
rm(df)
wrds_user <- cfg$wrds_user
wrds_pwd <- cfg$wrds_pwd
rm(cfg)
} else if(rstudioapi::isAvailable()) {
wrds_user <- rstudioapi::askForPassword(prompt="Enter your WRDS username")
wrds_pwd <- rstudioapi::askForPassword()
} else if(!rstudioapi::isAvailable()) {
stop("
cannot establish connection to WRDS (connect_WRDS.R)
- missing WRDS credentials (wrds_user & wrds_pwd)
- see _config.csv for troubleshooting")
}
#
#
# --- WRDS Connection ----------------------------------------------------------
wrds <- dbConnect(
Postgres(),
host = 'wrds-pgdata.wharton.upenn.edu',
port = 9737,
user = wrds_user,
password = wrds_pwd,
sslmode = 'require',
dbname = 'wrds'
)
rm(wrds_user)
rm(wrds_pwd)
message("Logged on to WRDS!")
#
#
54 changes: 22 additions & 32 deletions code/R/pull_wrds_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
#
# This code pulls data from WRDS
# ------------------------------------------------------------------------------

#
library(RPostgres)
library(DBI)

if (!exists("cfg")) source("code/R/read_config.R")

save_wrds_data <- function(df, fname) {
#
save_rds_versionctrl <- function(df, fname) {
if(file.exists(fname)) {
file.rename(
fname,
Expand All @@ -21,23 +19,13 @@ save_wrds_data <- function(df, fname) {
}
saveRDS(df, fname)
}

# --- Connect to WRDS ----------------------------------------------------------

wrds <- dbConnect(
Postgres(),
host = 'wrds-pgdata.wharton.upenn.edu',
port = 9737,
user = cfg$wrds_user,
password = cfg$wrds_pwd,
sslmode = 'require',
dbname = 'wrds'
)

message("Logged on to WRDS ...")

#
# --- Connection to WRDS -------------------------------------------------------
source("code/R/connect_WRDS.R")
#
#
# --- Specify filters and variables --------------------------------------------

#
dyn_vars <- c(
"gvkey", "conm", "cik", "fyear", "datadate", "indfmt", "sich",
"consol", "popsrc", "datafmt", "curcd", "curuscn", "fyr",
Expand All @@ -49,25 +37,25 @@ dyn_vars <- c(
"recch", "invch", "apalch", "txach", "aoloch",
"gdwlip", "spi", "wdp", "rcp"
)

#
dyn_var_str <- paste(dyn_vars, collapse = ", ")

#
stat_vars <- c("gvkey", "loc", "sic", "spcindcd", "ipodate", "fic")
stat_var_str <- paste(stat_vars, collapse = ", ")

#
cs_filter <- "consol='C' and (indfmt='INDL' or indfmt='FS') and datafmt='STD' and popsrc='D'"


#
#
# --- Pull Compustat data ------------------------------------------------------

#
message("Pulling dynamic Compustat data ... ", appendLF = FALSE)
res <- dbSendQuery(wrds, paste(
"select",
dyn_var_str,
"from COMP.FUNDA",
"where", cs_filter
))

#
wrds_us_dynamic <- dbFetch(res, n=-1)
dbClearResult(res)
message("done!")
Expand All @@ -76,13 +64,15 @@ message("Pulling static Compustat data ... ", appendLF = FALSE)
res2<-dbSendQuery(wrds, paste(
"select ", stat_var_str, "from COMP.COMPANY"
))

#
wrds_us_static <- dbFetch(res2,n=-1)
dbClearResult(res2)
message("done!")

#
wrds_us <- merge(wrds_us_static, wrds_us_dynamic, by="gvkey")
save_wrds_data(wrds_us, "data/pulled/cstat_us_sample.rds")

save_rds_versionctrl(wrds_us, "data/pulled/cstat_us_sample.rds")
#
dbDisconnect(wrds)
rm(wrds)
message("Disconnected from WRDS")
#
17 changes: 0 additions & 17 deletions code/R/read_config.R

This file was deleted.