****************************************************************
Access WRDS Data Using SAS/R
****************************************************************

1. SAS code

# Establishing a connection

%let wrds = wrds.wharton.upenn.edu 4016;
options comamid=TCP;
signon wrds username=_prompt_;

# Querying Data

rsubmit;
libname crsp "/wrds/crsp/sasdata/a_stock";
data Price;
      set crsp.dsf (keep= permno permco cusip date hsiccd prc ret shrout);
      by permno date;
      where mdy(01,01, 1996)<=date<=mdy(12,31,2001);
run;

2. R code

# Establishing a connection

library(RPostgres)
wrds <- dbConnect(Postgres(),
      host='wrds-pgdata.wharton.upenn.edu',
      port=9737,
      user = '********',
      password = '**********',
      dbname='wrds',
      sslmode='require')

# Querying Data

res <- dbSendQuery(wrds, "select cusip,permno,date,bidlo,askhi
      from crsp.dsf
      where askhi > 2500
     and bidlo < 2000")
data <- dbFetch(res, n=15)
dbClearResult(res)
data