sas - linking crsp and compustat in R via WRDS -
i using r connect wrds. now, link compustat , crsp tables. in sas, achieved using macros , ccm link table. best way approach topic in r?
progress update:
i downloaded crsp, compustat , ccm_link tables wrds.
sql <- "select * crsp.ccmxpf_linktable" res <- dbsendquery(wrds, sql) ccmxpf_linktable <- fetch(res, n = -1) ccm.dt <- data.table(ccmxpf_linktable) rm(ccmxpf_linktable)
i converting suggested matching routine wrds event study sas file r:
ccm.dt[,typeflag:=linktype %in% c("lu","lc","ld","ln","ls","lx") & usedflag=="1"] setkey(ccm.dt, gvkey, typeflag) (i in 1:nrow(compu.dt)) { gvkey.comp = compu.dt[i, gvkey] endfyr.comp = compu.dt[i,endfyr] permno.val <- ccm.dt[.(gvkey.comp, true),][linkdt<=endfyr.comp & endfyr.comp<=linkenddt,lpermno] if (length(permno.val)==0) permno.val <- na suppresswarnings(compu.dt[i, "permno"] <- permno.val) }
however, code fantastically inefficient. started out data.table, not understand how apply logic in for-loop. hoping point me way how improve for-loop.
matching fields in stages works better. maybe finds useful. suggestions further improvement of course welcome!!!
# filter on ccm.dt ccm.dt <- ccm.dt[linktype %in% c("lu","lc","ld","ln","ls","lx") & usedflag=="1"] setkey(ccm.dt, gvkey) setkey(compu.dt, gvkey) compu.merged <- merge(compu.dt, ccm.dt, all.x = true, allow.cartesian = true) # deal nas in linkenddt - set nas todays date, assuming still exist. today <- as.character(sys.date()) compu.merged[is.na(linkenddt), "linkenddt":=today] # filter out date mismatches compu <- compu.merged[linkdt <= endfyr & endfyr<=linkenddt]
Comments
Post a Comment