Last active
August 29, 2015 13:57
-
-
Save edwinhu/9397971 to your computer and use it in GitHub Desktop.
Macro to download and parse the Fama French Factors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*******************READ ME********************************************* | |
* - Macro to download and parse the Fama French Factors - | |
* | |
* SAS VERSION: 9.4.0 | |
* DATE: 2014-03-06 | |
* AUTHOR: eddyhu at the gmails | |
* | |
****************END OF READ ME******************************************/ | |
%macro GET_FFDATA(file=F-F_Research_Data_Factors, | |
dsetout=ff_factors, | |
align='E', | |
local= | |
); | |
/***************************************************************** | |
* MACRO: GET_FFDATA() | |
* GOAL: Download and parse FF factors | |
* PARAMETERS: file = Fama-French file to parse (see Kenneth French's website) | |
* dsetout = output dataset (default ff_factors) | |
* align = alignment of FF date within month (default 'E') | |
* NOTE: Does not work yet with weekly or daily versions | |
* | |
*****************************************************************/ | |
%let frenchftpurl = http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/; | |
%let local = %sysfunc(getoption(work)); | |
%if %NRBQUOTE(&file.)=%NRBQUOTE(F-F_Research_Data_Factors) %then %let first=5; | |
%if %NRBQUOTE(&file.)=%NRBQUOTE(F-F_Research_Data_Factors_weekly) %then %let first=6; | |
%if %NRBQUOTE(&file.)=%NRBQUOTE(F-F_Research_Data_Factors_daily) %then %let first=6; | |
/* Set remote and local file locations */ | |
FILENAME zipurl URL "&frenchftpurl.&file..zip"; | |
FILENAME ziploc "&local.&file..zip"; | |
FILENAME zip ZIP "&local.&file..zip"; | |
/* Download file */ | |
data _null_; | |
infile zipurl recfm=n; | |
input; | |
file ziploc; | |
put _infile_; | |
run; | |
/* Create file */ | |
proc sql; | |
drop table &dsetout.; | |
quit; | |
data &dsetout.; | |
INFORMAT date yymmn6.; | |
FORMAT date yymmn6.; | |
INFILE zip(&file..txt) | |
STOPOVER | |
FIRSTOBS=&first. | |
DELIMITER=' ' | |
TERMSTR=CRLF | |
; | |
INPUT date MktRF SMB HML RF ; | |
run; | |
/* Adjust dates to end of month */ | |
%if %UPCASE(&align.)='B' OR %UPCASE(&align.)='M', | |
OR %UPCASE(&align.)='E' OR %UPCASE(&align.)='S' %then %do; | |
data &dsetout.;set &dsetout.; | |
month=put(month(date),z2.); | |
year=year(date); | |
date = intnx('month', | |
input(cat(year,"-",month,"-","01"), | |
yymmdd10.),0,&align.); | |
format date yymmdd10.; | |
drop month year; | |
run; | |
%end; | |
/* Delete local file */ | |
/* DATA _NULL_ ;*/ | |
/* rc = FDELETE('ziploc');*/ | |
/* RUN ;*/ | |
FILENAME zipurl CLEAR; | |
FILENAME ziploc CLEAR; | |
FILENAME zip CLEAR; | |
%mend GET_FFDATA; | |
*'; *"; *); */; %mend; run; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment