Skip to content

Instantly share code, notes, and snippets.

@joosti
joosti / sas_export_to_mongodb.sas
Last active August 29, 2015 14:16
SAS export to Mongodb
/*
Export of SAS dataset to Mongodb
*/
/* References to project folders */
%let projectDir = E:\research\projects\myProject\;
%let mongoExportDir = &projectDir.sas\export to mongo\;
/* Path to Mongoimport executable */
%let mongoimportPath = 'C:\Program Files\MongoDB\Server\3.0\bin\mongoimport.exe';
@joosti
joosti / SAS_variables_funda.sas
Created January 27, 2015 16:50
SAS variables in Compustat Fundamental Annual
/* Retrieve variables in Funda */
ods listing close;
ods output variables = varsFunda;
proc datasets lib = comp; contents data=funda; quit;run;
ods output close;
ods listing;
/* keep relevant variables (excluding firm name, gvkey, fyear, etc)*/
data varsFunda ;
set varsFunda;
@joosti
joosti / SAS_syslput_rsubmit.sas
Created January 26, 2015 23:34
SAS Macro variables in remote submit (rsubmit) using SYSLPUT
/* Making macro variables available on remote server with syslput */
/* login to remote server */
%let wrds = wrds.wharton.upenn.edu 4016;options comamid = TCP remote=WRDS;
signon username=_prompt_;
/* this macro retrieves Compustat Funda variables from year1 to year2 */
%macro getFunda(dsout=, year1=2010, year2=2013, vars=);
@joosti
joosti / SAS_IBES_number_of_analysts.sas
Last active December 8, 2023 11:05
SAS IBES number of analysts following
/* IBES: number of analysts
*/
data a_funda (keep = key gvkey fyear datadate conm);
set comp.funda;
/* limit to firms with more than $20 mln sales and fiscal years 2010-2012 */
if sale > 20;
if 2010 <= fyear <= 2012;
/* create key to uniquely identify firm-year */
key = gvkey || fyear;
@joosti
joosti / SAS_macro_ccm_crsp_compustat_matched.sas
Created January 22, 2015 17:35
SAS CCM CRSP Compustat matched
/* macro that retrieves permno for Compustat records
dsin: dataset to append permno to, required variables: gvkey, and a date variable
dsout: dataset to create
datevar: name of date variable (e.g. datadate, or event date)
sample use: %getPermno(dsin=a_mydata, dsout=b_withpermno, datevar=datadate);
*/
%macro getPermno(dsin=, dsout=, datevar=);
proc sql;
@joosti
joosti / SAS_fixed_effects.sas
Created January 20, 2015 16:56
SAS fixed effects regression
/* requires sorting */
proc sort data = myData; by gvkey;run;
/* glm */
proc glm data = myData;
absorb gvkey;
ods output ParameterEstimates = work.params
FitStatistics = work.fit
NObs = work.obs;
model depvar = indep1 indep2 / solution ;
@joosti
joosti / SAS_assignment_week_2_most_recent_year.sas
Last active August 29, 2015 14:13
SAS assignment week 2 - most recent year
data returns;
input @01 id
@03 date MMDDYY10.
@14 return;
format date date9.;
datalines;
1 10/31/2013 0.01
1 11/30/2013 0.02
1 12/31/2013 0.03
1 01/31/2014 -0.01
@joosti
joosti / SAS_assignment_week_1_ratings.sas
Last active August 29, 2015 14:13
SAS assignment week 1 - ratings
data ratingdata;
input @01 id 1.
@03 startyr 4.
@08 endyr 4.
@13 rating $1.
;
datalines;
1 2002 2004 A
1 2005 2007 A
1 2007 2009 B
@joosti
joosti / sas_assignment_week1_ratings.csv
Created January 9, 2015 15:49
SAS assignment week 1 - combining ratings, results
id rating_combined start_combined end_combined
1 A 2002 2007
1 B 2007 2009
1 A 2009 2010
2 A 2002 2004
3 B 2001 2007
@joosti
joosti / sas_crsp_dsenames.sas
Created January 1, 2015 21:53
SAS CRSP DSENAMES
/* Retrieve historic cusip
work.myDset holds permno and datadate (end of fiscal year)
Appends historic cusip (at time datadate)
*/
proc sql;
create table work.withCusip as
select a.*, b.ncusip
from work.myDset a, crsp.dsenames b
where
a.permno = b.PERMNO