/* STEP 1: Find the permno corresponding to tickers */

data crspsel;
  set crsp.msfnames;
  where ticker in ("ENE" "ADP" "FDC");

proc print data=crspsel;

/* At this stage, you may want to check why some PERMNOs had more than */
/* one entry in msfnames by querying the evenst file                   */

data events;
 set crsp.mse;
 where ticker in ("ENE" "ADP" "FDC");

proc print data=events;
  var ticker permno comnam event date shrcd shrcls exchcd;

/* STEP 2: Eliminate duplicate PERMNOs */

proc sort data=crspsel nodupkey;
  by permno;

/* STEP 3: Get data from the monthly stock file */

proc sql;
 create table crspsel2 as select
  crspsel.permno, crspsel.ticker, msf.date, msf.prc, msf.ret, msf.vol
  from crspsel, crsp.msf
  where crspsel.permno=msf.permno and msf.date ge '31dec98'D;
quit;

proc print data=crspsel2 (obs=20);

/* STEP 4: Get value-weighted return */

proc sql;
 create table crspsel3 as select
 crspsel2.*, msi.vwretd, qtr(crspsel2.date) as qtr, year(crspsel2.date) as year
 from crspsel2, crsp.msi
 where crspsel2.date=msi.date;
quit;

proc print data=crspsel3 (obs=20);

/* STEP 5: Go back to previous SQL step and ad qtr and year */

/* STEP 6: Get the Compustat gvkeys from cstlink */

proc sql;
 create table crspsel3 as select
 crspsel3.*, cstlink.gvkey
 from crspsel3, crsp.cstlink
 where crspsel3.permno=cstlink.npermno;
quit;

proc print data=crspsel3 (obs=20);

/* Get Compustat data items from merged CRSP-Compustat */

proc sql;
 create table finaldata as select
 crspsel3.*, cstqtr.date as cstdate, cstqtr.data44, cstqtr.dnum, cstqtr.rdqe
 from crspsel3, crsp.cstqtr
 where crspsel3.gvkey=cstqtr.gvkey and crspsel3.year=cstqtr.year and crspsel3.qtr=cstqtr.qtr;
quit;

proc print data=finaldata (obs=60);
 var permno ticker gvkey date year qtr cstdate prc rdqe data44;